1

I have some interesting problem and can't find the solution. Look at this:

var d1 = new Date("07 31 2014");
document.write(d1);
document.write('<br />');
var d2 = new Date(1406746800 * 1000);
document.write(d2);

when I run this script I get this result:

Thu Jul 31 2014 00:00:00 GMT+0500 (UZT)
Thu Jul 31 2014 00:00:00 GMT+0500 (UZT)

then after I changed my time zone I get this result:

Thu Jul 31 2014 00:00:00 GMT-0800 (AKDT)
Wed Jul 30 2014 11:00:00 GMT-0800 (AKDT)

as you can see the second result is Jul 30, but first result is Jul 31. I think they must both be equal to 31 Jul. I know this problem depends on the timezone but is there a solution?

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
user2883814
  • 365
  • 2
  • 18
  • Hello - i guess its Thursday 31 00:00 but with +0500 in that specific Timezone. So if you calculate with this Timestamp for another Date with -0800 you should get the Date 13 Hours before ( from 24:00 - 13 = 11:00) - and so if your calculating with a Timestamp for a Specific ZimeZone you get different Values – derdida Jul 30 '14 at 07:39
  • Use UTC dates maybe? http://stackoverflow.com/questions/9756120/utc-timestamp-in-javascript – Ilya Luzyanin Jul 30 '14 at 07:41

1 Answers1

2

So the constructor parameter is:

an Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC

So given your integer value, that represents (for me, in BST):

Wed Jul 30 2014 20:00:00 GMT+0100

Which is

Wed Jul 30 2014 19:00:00 UTC

And your timezone is GMT-8, so thats the above -8 which gives:

Wed Jul 30 2014 11:00:00 GMT-0800 AKDT

The date string constructor constructs the date in your local timezone. You can see what the value should be by doing this:

new Date("07 31 2014").getTime() / 1000

Which returns 1406761200, not 1406746800

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176