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?

iamsumesh
  • 646
  • 4
  • 8
  • 22

1 Answers1

11

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.

Community
  • 1
  • 1
  • 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 – iamsumesh Jul 01 '15 at 14:12
  • you can use this method to convert a date from a `Date() object`, but not from a `IST string`, see my answer edit. – Walter Chapilliquen - wZVanG Jul 01 '15 at 14:43