Jo Smo Jo Smo - 11 days ago 6
Javascript Question

How to convert UTC datetime (string) into the visitors timezone?

How to convert a date like this:

2012-07-16 01:00:00 +00
(it's in the
UTC +00:00
timezone) to the visitors timezone in Javascript, ensuring that daylight saving will be handelled correctly?

Answer

Break it into parts and use Date.UTC, e.g.

function fromUTC(s) {
  s = s.split(/[-: ]/g);
  return new Date(Date.UTC(+s[0], --s[1], +s[2], +s[3], +s[4], +s[5], 0));
}

var s = '2012-07-16 01:00:00 +00';

alert(fromUTC(s));
Comments