moment js set timezone code example

Example 1: moment js convert to local timezone

// MOMENT.JS IS NO LONGER MAINTAINED - SWITCH TO DAY.JS, LUXON, ETC.

const time = moment.tz("2021-04-14T02:08:10.370Z")
const localtz = moment.tz.guess()
const date = time.clone().tz(localtz)
const formatDate = moment(date).format('MMMM Do YYYY, h:mm:ss A z')

Example 2: moment date without timezone

moment().utcOffset(0, true).format()

Example 3: moment get timezone

var tz = moment.tz.guess();

Example 4: momentTimeZone

var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london     = newYork.clone().tz("Europe/London");

newYork.format();    // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format();     // 2014-06-01T17:00:00+01:00

Tags:

Misc Example