Reputation: 4770
All the problem starts when I detect an odd behavior: I had a long number that means "the number of milliseconds since 0001/01/01", then in C# date time when I use the AddMilliseconds
get a different date value that the one returned from the moment one, in one hour different. eg.
new DateTime().AddMilliseconds(63613091700000); => {10/26/2016 3:15:00 PM}
moment([1]).add(63613091700000).toDate() => Wed Oct 26 2016 16:15:00 GMT-0400 (Eastern Daylight Time)
In C# get the 15:15h and in the moment 16:15!!!
That blown my mind, so I search the error, and I find it:
moment([1]).toDate() => Mon Jan 01 1 00:00:00 GMT-0500 (Eastern Standard Time)
The problem was that when I create the moment from a custom date (moment([1])
) it use the GMT-0500 (Eastern Standard Time)
but when apply the moment add
method it returns GMT-0400 (Eastern Daylight Time)
! Also check that creating the moment date by moment()
or using the javascript date new Date()
also use the GMT-0400 (Eastern Daylight Time)
. So that is the problem.
My question is, why does this happen? Is it an issue?
Upvotes: 0
Views: 907
Reputation: 147483
ECMAScript treats daylight saving rules as if they always applied, even before daylight saving was introduced.
When you create a Date for 0001-01-01 in host set to US EST time zone, there was no daylight saving (because US EDT runs from March to early November) so a UTC time value is calculated for 0001-01-01T00:00:00-0500 or 0001-01-01T05:00:00Z (they are the same moment in time).
On the same host, when you add enough milliseconds to get to 2016-10-26 15:15:00-0500 in a place that has daylight saving on that day, then the time will increment 1 hour because of the different offset for daylight saving, so the time is shown as 16:15:00 or in full: 2016-10-26T16:15:00-0400.
But note that 2016-10-26T15:15:00-0500 and 2016-10-26T16:15:00-0400 are exactly the same moment in time (and equivalent to 2016-10-26T20:15:00Z), the only difference is the timezone offset.
If you want to display dates and times for a particular timezone other than UTC, use moment-timezone.js.
Upvotes: 1
Reputation: 4770
I have found what is happening, but not how to handle it:
The problem is the Is Daylight Saving Time
of javascript.
From http://momentjs.com/docs/#/query/is-daylight-saving-time/:
moment([2011, 2, 12]).isDST(); // false, March 12 2011 is not DST
moment([2011, 2, 14]).isDST(); // true, March 14 2011 is DST
The thing is that when I do moment([1]).add(63613091700000).toDate()
it calculates in the right way, but the result datetime is in DST so it transform it. I doesn't want that behavior this time.
Upvotes: 0