TheKingPinMirza
TheKingPinMirza

Reputation: 8922

Javascript/Json date conversion issue

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?

Upvotes: 3

Views: 116

Answers (1)

Omri Aharon
Omri Aharon

Reputation: 17064

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.

Upvotes: 1

Related Questions