Reputation: 9371
I have an array called dateArr which contains: [2014, 07, 10];
When I create a date out of it like below:
new Date(dateArr[0], Number(dateArr[1]), Number(dateArr[2]), 0, 0, 0);
It evaluates to '2014-08-09T23:00:00.000Z' in the console. I understand thats months are zero based, but why, to correct the time, do I have to do this:
new Date(dateArr[0], Number(dateArr[1]) - 1, Number(dateArr[2]), 1, 0, 0);
Which gives: 2014-07-10T00:00:00.000Z
This doesn't seem right to me, could someone explain?
Upvotes: 0
Views: 74
Reputation: 14354
new Date(2014, 6, 10, 0, 0, 0)
uses your current timezone (which is +0100 for you, and, e.g. +0400 for me in Moscow). So it's result will depends on timezone where you run the code.
> new Date(2014, 6, 10)
Thu Jul 10 2014 00:00:00 GMT+0400 (MSK)
> new Date(2014, 6, 10).toISOString()
'2014-07-09T20:00:00.000Z'
if you really want midnight in «ISO time» you could use constructor with string:
> new Date('2014-06-10Z').toISOString()
'2014-06-10T00:00:00.000Z'
Upvotes: 2
Reputation: 3514
Your tools are working with different timezones. (chrome dev tools output indicates that by adding GMT+0100(BST) to your output and therefore converting it to BST (British summer time) While webstorm gives you a utc/gmt time. In fact 2014-07-09T23:00:00.000 is exactly the same time as Thu Jul 10 2014 00:00:00 GMT+0100 (BST).
You may use Date.UTC()
to generate a correct timestamp (but this will then be one hour off into the other direction in your local time)
I tried that within my own chrome developer console:
> new Date(Date.UTC(2014, 6, 10, 0, 0, 0))
Thu Jul 10 2014 02:00:00 GMT+0200 (MESZ) //middle european summer time
Upvotes: 1