Skip to content

Instantly share code, notes, and snippets.

@niraj-shah
Last active November 21, 2016 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niraj-shah/de88f00220bd3c793d097b1b1f9d65b1 to your computer and use it in GitHub Desktop.
Save niraj-shah/de88f00220bd3c793d097b1b1f9d65b1 to your computer and use it in GitHub Desktop.
Timezone support using Moment.js
// get the user's timezone
var tz = moment.tz.guess();
console.info('Timezone: ' + tz);
// returns: Timezone: Europe/London
// set the default user timezone
moment.tz.setDefault(tz);
// set custom timezone
moment.tz.setDefault('America/Los_Angeles');
// convert date / time to local timezone, assumes original date/time is in UTC
moment.utc('2016-12-25 07:00').tz(tz).format('ddd, Do MMMM YYYY, h:mma');
// returns: Sun, 25th December 2016, 7:00am
// convert date/time to LA Time
moment.utc('2016-12-25 07:00').tz('America/Los_Angeles').format('ddd, Do MMMM YYYY, h:mma');
// returns: Sat, 24th December 2016, 11:00pm
// convert from LA time to London
moment.tz('2016-12-25 07:00', 'America/Los_Angeles').tz('Europe/London').format( 'ddd, Do MMMM YYYY, h:mma' );
// returns: Sun, 25th December 2016, 3:00pm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment