Recently I worked on something that should be a simple and straightforward task. Timestamp was sent as string from backend and it should converted it to Date object in fronted. Format of the string looked like this -
2015-02-08 18:00:00. No biggie, right?
I’ve checked MDN and saw that Date accepts
dateString as a argument. Without digging further I’ve tried in Chrome console.
It worked, task was finished and sent for testing. Soon after I got it back because of the issues in Safari and Internet Explorer. Safari console reported
Now, let’s go back to the basics. I’ve reread the docs (this time for real) noticed that T letter was missing in date format. Standardized IETF format would be
2015-02-08T18:00:00. So first solution could be something simple as using replace
'2015-02-08 18:00:00'.replace(' ', 'T').
Another solution I had in mind was to split the string into an array and pass it as argument to
Date constructor. So simple
apply should do the trick.
Ok, it’s not going to be that easy. After a while, I figured out I that can use
bind as a wrapper around constructor and pass in sliced arguments to the
Date context constructor. That turned out something like this:
Let’s clarify what this line actually does:
- I used Underscore’s bind because of few reasons firstly – it was already loaded in the project and secondly – old browsers don’t support native
- Split accepts
RegExas a separator. So
.split(/[\-\s\:]/)splits string on dash, space and colon. In our case we get
['2015', '02', '08', '18', '00', '00']
[Date, null].concat…this part is interesting. Bind accepts two or more parametars. First one is our
Dateconstructor, second one is context which is in our case
null. Rest of the arguments are passed into first function, our constructor.
I remembered what a friend of mine would say:
Dude, don’t be stupid, just use a library! Moment.js is awesome.
And I think he is probably right. But then again I feel like it’s such overkill to include a 12kb library for such simple task.