Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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