this is my perfect moment.js with you

The JavaScript Date library sucks. Everyone knows this… But on the face of it, things seem legit. Let’s see why Thou shalt use moment.js.

“it’s ok, i got this m8”

JavaScript sorts timezones out for you:

new Date('2015-03-29 00:00:00')
Sun Mar 29 2015 00:00:00 GMT+0000 (GMT)
new Date('2015-03-29 01:00:00')
Sun Mar 29 2015 02:00:00 GMT+0100 (BST)

But it shouldn’t. I should pick when I want a (albeit flaky) timezone specific representation thanks.

“i think we change times… now?”

It’s OK though, because we know that here in the UK, daylight saving starts at 1am. Creating a timeline like:

12am 1am 1am 2am

JavaScript isn’t all about that though.

new Date('2015-03-29 00:00:00')
Sun Mar 29 2015 00:00:00 GMT+0000 (GMT)
new Date('2015-03-29 01:00:00')
Sun Mar 29 2015 02:00:00 GMT+0100 (BST)
new Date(2015-03-29 02:00:00)
Sun Mar 29 2015 02:00:00 GMT+0100 (BST)

moment.js

Download it, use it, embrace it.

new moment('2015-03-29 00:00:00').utc().format()
"2015-03-29T00:00:00+00:00"
new moment('2015-03-29 01:00:00').utc().format()
"2015-03-29T01:00:00+00:00"
new moment('2015-03-29 02:00:00').utc().format()
"2015-03-29T01:00:00+00:00"

OK, OK… What about:

new Date('2015-03-29 01:00:00').getHours()
2
new moment('2015-03-29 01:00:00').utc().hours()
1

mic drop

blog comments powered by Disqus