Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display date/time in user's locale format and time offset

I want the server to always serve dates in UTC in the HTML, and have JavaScript on the client site convert it to the user's local timezone.

Bonus if I can output in the user's locale date format.

like image 942
kch Avatar asked Sep 17 '08 16:09

kch


People also ask

How do I get a time zone offset?

Definition and UsagegetTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.

How do I get a locale date?

Use the toLocaleString() method to get a date and time in the user's locale format, e.g. date. toLocaleString() . The toLocaleString method returns a string representing the given date according to language-specific conventions.

How do I find the timezone in an ISO date?

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .

What is locale date string?

The toLocaleDateString() method returns a string with a language-sensitive representation of the date portion of the specified date in the user agent's timezone. In implementations with Intl. DateTimeFormat API support, this method simply calls Intl. DateTimeFormat .


2 Answers

Seems the most foolproof way to start with a UTC date is to create a new Date object and use the setUTC… methods to set it to the date/time you want.

Then the various toLocale…String methods will provide localized output.

Example:

// This would come from the server.  // Also, this whole block could probably be made into an mktime function.  // All very bare here for quick grasping.  d = new Date();  d.setUTCFullYear(2004);  d.setUTCMonth(1);  d.setUTCDate(29);  d.setUTCHours(2);  d.setUTCMinutes(45);  d.setUTCSeconds(26);    console.log(d);                        // -> Sat Feb 28 2004 23:45:26 GMT-0300 (BRT)  console.log(d.toLocaleString());       // -> Sat Feb 28 23:45:26 2004  console.log(d.toLocaleDateString());   // -> 02/28/2004  console.log(d.toLocaleTimeString());   // -> 23:45:26

Some references:

  • toLocaleString
  • toLocaleDateString
  • toLocaleTimeString
  • getTimezoneOffset
like image 145
kch Avatar answered Sep 19 '22 21:09

kch


You can do it with moment.js (deprecated in 2021)

It's best to parse your date string from UTC as follows (create an ISO-8601 compatible string on the server to get consistent results across all browsers):

var m = moment("2013-02-08T09:30:26Z"); 

Now just use m in your application, moment.js defaults to the local timezone for display operations. There are many ways to format the date and time values or extract portions of it.

You can even format a moment object in the users locale like this:

m.format('LLL') // Returns "February 8 2013 8:30 AM" on en-us 

To transform a moment.js object into a different timezone (i.e. neither the local one nor UTC), you'll need the moment.js timezone extension. That page has also some examples, it's pretty simple to use.

Note: Moment JS recommends more modern alternatives, so it is probably not a good choice for new projects.

like image 23
theDmi Avatar answered Sep 17 '22 21:09

theDmi