Last active
November 21, 2016 22:03
-
-
Save niraj-shah/de88f00220bd3c793d097b1b1f9d65b1 to your computer and use it in GitHub Desktop.
Timezone support using Moment.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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