0

I am trying to convert a date, but it shows differently on a set date between time zones.

new Date(1404100800000)
Mon Jun 30 2014 00:00:00 GMT-0400 (Eastern Daylight Time)

new Date(1404100800000)
Sun Jun 29 2014 21:00:00 GMT-0700 (Pacific Daylight Time)

Why does it do that? I must be not understanding something about dates.

xivo
  • 2,054
  • 3
  • 22
  • 37
  • where's the rest of your code? – Daniel A. White Aug 21 '14 at 15:22
  • That is my code, I ran it off the console in the latest Chrome – xivo Aug 21 '14 at 15:22
  • not able to reproduce over here in chrome. – Daniel A. White Aug 21 '14 at 15:22
  • 4
    If you look closely, you will notice that it's the exact same time in both cases, but expressed in the current timezone (just add 4 and 7 hours to each time and you get the exact datetime). `1404100800000` is the time in UTC. You have to explain better what your problem is. Do you want `1404100800000` to be treated as *local* time? – Felix Kling Aug 21 '14 at 15:23
  • I want the date to be treated as the day it was set. This is the time stamp returning. It is saved without datetime. It should return the same date? Its why I ask if I am not understanding something. If I were to save 6/30, why would it return 6/29? – xivo Aug 21 '14 at 15:26
  • that is the same date time. – Daniel A. White Aug 21 '14 at 15:29
  • As I already tried to explain, it returns it is the exact same datetime in both cases. `Mon Jun 30 2014 4:00:00 UTC` (which is what `1404100800000` is), is `Mon Jun 30 2014 00:00:00 GMT-0400` in EDT and `Sun Jun 29 2014 21:00:00 GMT-0700` in PDT. It's one fixed point in time. If you want to treat `1404100800000` as *local time*, every timezone will get a different time, because 4am in EDT is at a different moment than 4am in PDT. – Felix Kling Aug 21 '14 at 15:29
  • They are the same date and time at UTC (that's what the timestamp is) - a fixed time at the prime meridian - It is always Mon Jun 30 2014 04:00:00. Outputting this in different timezones will give you different local times (and days). – Code Uniquely Aug 21 '14 at 15:30

1 Answers1

1

This is expected. The timestamp number is measuring time independent of timezone. When converted to human readable format, it will change based on the timezone of the system. This is exactly as you'd expect, so that an event that occured at 6pm in New York will not also be an event at 6pm in San Fransisco ... but they will have the same timestamp.

From ECMA standard:

Time is measured in ECMAScript in milliseconds since 01 January, 1970 UTC. In time values leap seconds are ignored. It is assumed that there are exactly 86,400,000 milliseconds per day.

http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.1

Conversion to string is locale dependent:

This function returns a String value. The contents of the String are implementation-dependent, but are intended to represent the Date in the current time zone in a convenient, human-readable form.

http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.5.2

Tyler Peryea
  • 523
  • 4
  • 11
  • 2
    For ways to get around this see: http://stackoverflow.com/questions/439630/how-do-you-create-a-javascript-date-object-with-a-set-timezone-without-using-a-s – Devin H. Aug 21 '14 at 15:33