0

I am calling an API that returns a date/time string such as "2014-04-30 15:32:01". On top of this, I have a known timezone that this date/time exists in. I can see from the javascript Date() class has a .UTC() call for this, but that does not seem to accept a timezone as far as I can tell.

Given the date/time string + timezone, how can I convert those into a UTC timestamp?

Jim
  • 4,509
  • 16
  • 50
  • 80
  • I don't think there's any way to do that in javascript. If you have a timezone, something like `Eastern Daylight Time`, there's no way to get the actual difference between that timezone and UTC in javascript, I'm guessing you would have to either find an API that can return the timezone offset based on strings passed to it, or use the serverside to do it. – adeneo Apr 30 '14 at 19:50
  • take a look at http://momentjs.com/ – Sebastian Apr 30 '14 at 19:56
  • possible duplicate of [How do you convert a JavaScript date to UTC?](http://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc) – Chandrew Jun 16 '14 at 16:49

2 Answers2

0

I'd recommend using Moment.js and Moment Timezone.

dbratcher
  • 82
  • 5
0

You can create a timestamp using Date.parse from your local time (RFC2822 date format to include the timezone), create a date from that and use Date.toUTCString to get the UTC time. Not sure if it'll work with day light savings though.

Example: http://jsfiddle.net/3vXV6/ (will alert the date)

References:

NoGray
  • 1,149
  • 7
  • 9