6

How can we convert datetime string to UTC in javascript. We are getting following JSON from REST service

[  
   {  
      "CreationTime":"June 2, 2015 8:04:53 PM IST",
      "category":"UI",
      "severity":"MAJOR",
      "source":"BILLING",
      "status":"ASSIGNED"
   }
]

we are able to get CreationTime into a String variable but unable to convert to UTC. Any idea to convert this?

1 Answer 1

12

Using toUTCString():

var toUTC = new Date("June 2, 2015 8:04:53").toUTCString()

In Javascript you can use this method to convert a date from a Date() object, but not from a IST string. So you need format this string to a Date() object , then you can convert it to UTC. In this topic says what I mean.

Note If you try June 2, 2015 8:04:53 PM IST JavasScript take it as invalid date, for that you have to use .replace() function to remove the IST part of the string.

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

2 Comments

var dateString ="June 2, 2015 8:04:53 PM IST"; var toUTC = new Date(dateString).toUTCString() document.write(toUTC); i am getting output as Invalid Date. Any idea to solve this
you can use this method to convert a date from a Date() object, but not from a IST string, see my answer edit.

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.