3

I have following Java Script (Json) date format

data.d1: "2015-03-26T16:00:00.0000000"

I execute the following

data.d1 = new Date(data.d1);

It gives the following outcome which is wrong to me.

Thu Mar 26 2015 20:00:00 GMT+0400 (Arabian Standard Time)

It should return

Thu Mar 26 2015 16:00:00 GMT+0400 (Arabian Standard Time)

Why there is 4 hour difference? How i can get the same time (without 4 hours addition to me default time)? Any hint please

p.s. i can get exact time back by using following line of code

data.d1.setHours(data.d1.getHours() - 4);

Is this the only way?

TheKingPinMirza
  • 7,924
  • 6
  • 51
  • 81

1 Answers1

1

The 'T' in 2015-03-26T16:00:00.0000000 makes the Date constructor take UTC timezone into consideration. For you it's +4 hours, for me, for instance, it's +2 hours.

If you want the neutral time, you need to remove the 'T' from the string and you'll get the desired result: 2015-03-26 16:00:00.0000000

Fiddle

See this question if you want a pure JS solution without altering your string, it will work I've tested it.

Community
  • 1
  • 1
Omri Aharon
  • 16,959
  • 5
  • 40
  • 58
  • Omri, Well explained. one thing more, if i dont want to remove T from string then "data.d1.setHours(data.d1.getHours() - 4);" is the only way to get neutral time as per gulf region? Gulf is 4+ from GMT – TheKingPinMirza Mar 26 '15 at 12:32
  • 1
    @user1874957 That's might not be the best approach, since daylight time changes will screw it up, and also, what happens if someone not from the Gulf accesses your code? (the time interpretation is run locally) – Omri Aharon Mar 26 '15 at 12:38
  • @user1874957 I've updated my answer, added a link to a question that will solve your problem if you can't change the string (or don't want to). – Omri Aharon Mar 26 '15 at 12:44