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?

3
  • 2
    it is interpreting the string as UTC. Commented Mar 26, 2015 at 12:22
  • I edited my question, please have a look on my workaround. Commented Mar 26, 2015 at 12:23
  • I am getting Thu Mar 26 2015 21:30:00 GMT+0530 (IST) from India. So @DanielA.White is right, I believe so. Commented Mar 26, 2015 at 12:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
@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)
@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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.