6

I have a Date() object that contains a UTC date, which I need converted to the users local timezone. Does anybody know how I could do this? :-)

tarnfeld
  • 25,992
  • 41
  • 111
  • 146

4 Answers4

4

I usually create a new Date object and use the Date.setUTC* functions to copy the date information.

Radomir Dopieralski
  • 2,557
  • 17
  • 14
  • 1
    There is a script that searches the page for dates formatted as a date microformat and replaces them with user's local version: http://features.sheep.art.pl/LocalTimeAndDate – Radomir Dopieralski Sep 18 '10 at 10:47
  • Thanks, if you look at the javascript at the bottom of http://twtm.in/lw you'll see what I ended up doing :-) – tarnfeld Sep 18 '10 at 11:13
2

I'm pretty sure it is done for you automatically.

>>> d = new Date('Fri, 10 Jun 2011 19:49:23 UTC');
Sat Jun 11 2011 07:49:23 GMT+1200 (New Zealand Standard Time)
>>> d.getHours();
7
Benbob
  • 13,876
  • 18
  • 79
  • 114
0

This is an old thread, but just in case anyone else stumbles across this issue, here's how I got around this problem.

UTC dates from ASP.Net

In my example, I wanted my ASP.Net service to return dates in the user's local timezone, even though the date values were stored in SQL Server in the UTC timezone.

Community
  • 1
  • 1
Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
0

I found this for you: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC

The way you use it is: var x = Date.UTC(yyyy, mangmo, dd, hh, mi, ss, ms); // now x is a timestamp in milliseconds (it's a number, not a Date object) var y = new Date(x); // now y is the Date object you said you wanted Arguments to Date.UTC are as follows:
yyyy is the year (for example, 1984 or 2016)
mangmo is the mangled month (a number from 0 to 11)
dd is the day of the month (this isn't mangled, it's the same as on a wristwatch or wall calendar)
hh is the hour of the day (0 to 23)
mi is minutes, ss is seconds, ms is milliseconds (I don't think I need to explain these)

Robert Lozyniak
  • 322
  • 1
  • 7