Moment Timezone 中文文档

To use moment-timezone, you will need moment@2.9.0+, moment-timezone.js, and the moment-timezone data.

For convenience, there are builds available on with all the zone data or a subset of the data.

  • moment-timezone-with-data.js is recommended for server environments (Node.js) and covers all years available.
  • moment-timezone-with-data-10-year-range.js is recommend for most browser environments, covering +/- 5 years from the year published.
  • moment-timezone-with-data-1970-2030.js covers a 60 year range, for those that need more data but not the larger file size of the full data file.

If you use one of the above files, you still need moment.js, but you do not need moment-timezone.js because it is included.


npm install moment-timezone

In Node.js, all the data is preloaded. No additional code is needed for loading data.

var moment = require('moment-timezone');


<script src="moment.js"></script>
<script src="moment-timezone-with-data.js"></script>

When using Moment Timezone in the browser, you will need to load the data as well as the library.

You can either use the prebuilt library and data files linked on the homepage or build a subset of the data yourself and load it.



    paths: {
        "moment": "path/to/moment"
define(["path/to/moment-timezone-with-data"], function (moment) {

There are two interfaces for using time zones with Moment.js., String) does parsing in given time zone

It takes all the same arguments as the moment constructor, but uses the last argument as a time zone identifier:

var a ="2013-11-18 11:55", "Asia/Taipei");
var b ="2013-11-18 11:55", "America/Toronto");

a.format(); // 2013-11-18T11:55:00+08:00
b.format(); // 2013-11-18T11:55:00-05:00

a.utc().format(); // 2013-11-18T03:55Z
b.utc().format(); // 2013-11-18T16:55Z

Note that created moments have different UTC time because these moments were created in different time zones.

moment().tz(String) does converting to provided time zone

var a = moment("2013-11-18 11:55").tz("Asia/Taipei");
var b = moment("2013-11-18 11:55").tz("America/Toronto");

a.format(); // 2013-11-18T19:55:00+08:00
b.format(); // 2013-11-18T06:55:00-05:00

a.utc().format(); // 2013-11-18T11:55Z
b.utc().format(); // 2013-11-18T11:55Z

In this example, you first create moment("2013-11-18 11:55") object in your default timezone, and then change its timezone to specified.

Note that created moments have equal UTC time because these moments were created in a default timezone.

Parsing in Zone

edit, String);

The constructor takes all the same arguments as the moment constructor, but uses the last argument as a time zone identifier.

var a ="2013-11-18 11:55", "America/Toronto");
var b ="May 12th 2014 8PM", "MMM Do YYYY hA", "America/Toronto");
var c =, "America/Toronto");
a.format(); // 2013-11-18T11:55:00-05:00
b.format(); // 2014-05-12T20:00:00-04:00
c.format(); // 2014-06-22T12:21:08-04:00

This constructor is DST aware, and will use the correct offset when parsing."2013-12-01", "America/Los_Angeles").format(); // 2013-12-01T00:00:00-08:00"2013-06-01", "America/Los_Angeles").format(); // 2013-06-01T00:00:00-07:00

The offset is only taken into consideration when constructing with an array, string without offset, or object.

var arr = [2013, 5, 1],
    str = "2013-12-01",
    obj = { year : 2013, month : 5, day : 1 };, "America/Los_Angeles").format(); // 2013-06-01T00:00:00-07:00, "America/Los_Angeles").format(); // 2013-12-01T00:00:00-08:00, "America/Los_Angeles").format(); // 2013-06-01T00:00:00-07:00, "America/New_York").format();    // 2013-06-01T00:00:00-04:00, "America/New_York").format();    // 2013-12-01T00:00:00-05:00, "America/New_York").format();    // 2013-06-01T00:00:00-04:00

If the input string contains an offset, it is used instead for parsing. The parsed moment is then converted to the target zone.

var zone = "America/Los_Angeles";'2013-06-01T00:00:00',       zone).format(); // 2013-06-01T00:00:00-07:00'2013-06-01T00:00:00-04:00', zone).format(); // 2013-05-31T21:00:00-07:00'2013-06-01T00:00:00+00:00', zone).format(); // 2013-05-31T17:00:00-07:00

Unix timestamps and Date objects refer to specific points in time, thus it doesn't make sense to use the time zone offset when constructing. Using|Date, zone) is functionally equivalent to moment(Number|Date).tz(zone).

var timestamp = 1403454068850,
    date = new Date(timestamp);, "America/Los_Angeles").format(); // 2014-06-22T09:21:08-07:00
moment(timestamp).tz("America/Los_Angeles").format(); // 2014-06-22T09:21:08-07:00, "America/Los_Angeles").format();      // 2014-06-22T09:21:08-07:00
moment(date).tz("America/Los_Angeles").format();      // 2014-06-22T09:21:08-07:00

Уou may specify a boolean right after format argument to use strict parsing. Strict parsing requires that the format and input match exactly, including delimeters.'It is 2012-05-25', 'YYYY-MM-DD', "America/Toronto").isValid();       // true'It is 2012-05-25', 'YYYY-MM-DD', true, "America/Toronto").isValid(); // false'2012-05-25', 'YYYY-MM-DD', true, "America/Toronto").isValid();       // true'2012-05.25', 'YYYY-MM-DD', true, "America/Toronto").isValid();       // false

Parsing Ambiguities


Due to daylight saving time, there is a possibility that a time either does not exist, or has existed twice.

Spring Forward

In the spring, at the start of DST, clocks move forward an hour. In reality though, it is not time that is moving, it is the offset moving.

Moving the offset forward gives the illusion that an hour has disappeared. As the clock ticks, you can see it move from 1:58 to 1:59 to 3:00. It is easier to see what is actually happening when you include the offset.

1:58 -5
1:59 -5
3:00 -4
3:01 -4

The result is that any time between 1:59:59 and 3:00:00 never actually happened. Moment Timezone accounts for this. If you try to parse a time that never existed, it will skip forward by the amount of the DST gap (usually 1 hour)."2012-03-11 01:59:59", "America/New_York").format() // 2012-03-11T01:59:59-05:00"2012-03-11 02:00:00", "America/New_York").format() // 2012-03-11T03:00:00-04:00"2012-03-11 02:59:59", "America/New_York").format() // 2012-03-11T03:59:59-04:00"2012-03-11 03:00:00", "America/New_York").format() // 2012-03-11T03:00:00-04:00

In this example, the two o'clock hour doesn't exist, so it is treated as equivalent to the three o'clock hour.

Fall Back

In the fall, at the end of DST, clocks move backward an hour. Again, time is not moving backwards, only the offset is. In this case, the illusion is that an hour repeats itself.

Again, it is easier to see what is actually happening when you include the offset.

1:58 -4
1:59 -4
1:00 -5
1:01 -5

Moment Timezone handles this by always using the earlier instance of a duplicated hour."2012-11-04 00:59:59", "America/New_York"); // 2012-11-04T00:59:59-04:00"2012-11-04 01:00:00", "America/New_York"); // 2012-11-04T01:00:00-04:00"2012-11-04 01:59:59", "America/New_York"); // 2012-11-04T01:59:59-04:00"2012-11-04 02:00:00", "America/New_York"); // 2012-11-04T02:00:00-05:00

You won't be able to create a moment with the later instance of the duplicated hour unless you include the offset when parsing."2012-11-04 01:00:00-04:00", "America/New_York"); // 2012-11-04T01:00:00-04:00"2012-11-04 01:00:00-05:00", "America/New_York"); // 2012-11-04T01:00:00-05:00

Converting to Zone


The moment#tz mutator will change the time zone and update the offset.

moment("2013-11-18").tz("America/Toronto").format('Z'); // -05:00
moment("2013-11-18").tz("Europe/Berlin").format('Z');   // +01:00

This information is used consistently in other operations, like calculating the start of the day.

var m ="2013-11-18 11:55", "America/Toronto");
m.format();                     // 2013-11-18T11:55:00-05:00
m.startOf("day").format();      // 2013-11-18T00:00:00-05:00"Europe/Berlin").format(); // 2013-11-18T06:00:00+01:00
m.startOf("day").format();      // 2013-11-18T00:00:00+01:00

Without an argument, moment#tz returns:

  • the time zone name assigned to the moment instance or
  • undefined if a time zone has not been set.
var m ="2013-11-18 11:55", "America/Toronto");;  // America/Toronto
var m ="2013-11-18 11:55"); === undefined;  // true

Formatting Additions

edit"Z z"); // -08:00 PST;    // PST;    // PST

In addition to including the +00:00 formatting information, Moment Timezone also includes information for the abbreviated time zone name.[2012, 0], 'America/New_York').format('z');    // EST[2012, 5], 'America/New_York').format('z');    // EDT[2012, 0], 'America/Los_Angeles').format('z'); // PST[2012, 5], 'America/Los_Angeles').format('z'); // PDT

Note that these abbreviations may change depending on the time zone offset. This helps to distinguish offsets between places that may or may not use DST.

// Denver observes DST[2012, 0], 'America/Denver').format('Z z');  // -07:00 MST[2012, 5], 'America/Denver').format('Z z');  // -06:00 MDT
// Phoenix does not observe DST[2012, 0], 'America/Phoenix').format('Z z'); // -07:00 MST[2012, 5], 'America/Phoenix').format('Z z'); // -07:00 MST

Note also that these abbreviations are not globally unique. Below, you can see that both United States Central Standard Time and China Standard Time have the same abbreviation.'2016-01-01', 'America/Chicago').format('z');    // CST'2016-01-01', 'Asia/Shanghai').format('z');      // CST

You can also use moment#zoneAbbr to get the zone abbreviation. This is what moment.js uses when formatting the z token.[2012, 0], 'America/New_York').zoneAbbr(); // EST[2012, 5], 'America/New_York').zoneAbbr(); // EDT

Moment.js also provides a hook for the long form time zone name. Because these strings are generally localized, Moment Timezone does not provide any long names for zones.

To provide long form names, you can override moment.fn.zoneName and use the zz token.

var abbrs = {
    EST : 'Eastern Standard Time',
    EDT : 'Eastern Daylight Time',
    CST : 'Central Standard Time',
    CDT : 'Central Daylight Time',
    MST : 'Mountain Standard Time',
    MDT : 'Mountain Daylight Time',
    PST : 'Pacific Standard Time',
    PDT : 'Pacific Daylight Time',

moment.fn.zoneName = function () {
    var abbr = this.zoneAbbr();
    return abbrs[abbr] || abbr;
};[2012, 0], 'America/New_York').format('zz');    // Eastern Standard Time[2012, 5], 'America/New_York').format('zz');    // Eastern Daylight Time[2012, 0], 'America/Los_Angeles').format('zz'); // Pacific Standard Time[2012, 5], 'America/Los_Angeles').format('zz'); // Pacific Daylight Time

Please note that the z formatting token will not always show the abbreviated time zone name, instead, will show the time offsets for each region.'America/Los_Angeles').format('z')  // "PDT"     (abbreviation)'Asia/Magadan').format('z')         // "+11"     (3-char offset)'Asia/Colombo').format('z')         // "+0530"   (5-char offset)

Default time zone


By default, moment objects are created in the local time zone.
Local time zone - it's a time zone which is set in a browser or on your node.js server.

To change the default time zone, use with a valid time zone."America/New_York");

To reset the default time zone to local, use with no arguments.;

Subsequent calls to will not affect existing moment objects or their clones.

Guessing user zone


Moment Timezone uses the Internationalization API (Intl.DateTimeFormat().resolvedOptions().timeZone) in supported browsers to determine the user's time zone.

On other browsers, time zone detection is rather tricky to get right, as there is little information provided by those browsers. For those, it will use Date#getTimezoneOffset and Date#toString on a handful of moments around the current year to gather as much information about the browser environment as possible. It then compares that information with all the time zone data loaded and returns the closest match. In case of ties, the time zone with the city with largest population is returned.

By default Moment Timezone caches the detected timezone. This means that subsequent calls to will always return the same value.

You can call with an optional boolean argument "ignoreCache". If set to true, the cache will be ignored and overwritten with the new value.; // America/Chicago
// suppose the client's timezone changes to Europe/Berlin; // America/Chicago; // Europe/Berlin; // Europe/Berlin

Getting all Zones

edit; // String[]

To get a list of all available time zone names, use; // ["Africa/Abidjan", "Africa/Accra", "Africa/Addis_Ababa", ...]

Getting Zones for country

edit; // String[], { offset: true });

To get a list of time zones for some country, use'US');

By default this method returns zone names sorted alphabetically:

["America/Adak", "America/Anchorage", ... "Pacific/Honolulu"]

To get also offsets, pass { offset: true } as 2nd parameter:'CN', { offset: true });

it returns array of objects with name and offset:

   { name: "Asia/Shanghai", offset: -480 },
   { name: "Asia/Urumqi", offset: -360 }

It's useful if you need to sort time zones by offset.

All country codes can be retrieved using method

In order to match a timestamp to an offset, Moment Timezone uses a Zone object.

Though you shouldn't even need to use it, this object's constructor is available on the namespace.

This object has 4 properties.

    name    : 'America/Los_Angeles',          // the unique identifier
    abbrs   : ['PDT', 'PST'],                 // the abbreviations
    untils  : [1414918800000, 1425808800000], // the timestamps in milliseconds
    offsets : [420, 480]                      // the offsets in minutes


edit; // America/Los_Angeles

The uniquely identifying name of the time zone.


zone.abbr(timestamp); // PST

Get the abbreviation for a given timestamp from a Zone.'America/Los_Angeles').abbr(1403465838805); // PDT'America/Los_Angeles').abbr(1388563200000); // PST


zone.utcOffset(timestamp); // 480

Get the offset for a given timestamp from a Zone.'America/Los_Angeles').utcOffset(1403465838805); // 420'America/Los_Angeles').utcOffset(1388563200000); // 480

POSIX compatibility requires that the offsets are inverted. Therefore, Etc/GMT-X will have an offset of +X and Etc/GMT+X will have an offset of -X. This is a result of IANA's Time Zone Database and not an arbitrary choice by Moment.js. Thus, using locality based identifiers is preferred over fixed-offset identifiers.

For example, moment().tz('Etc/GMT+1').format('YYYY-MM-DD HH:mm ZZ') will return 2014-12-18 11:22 -0100 while moment().tz('Europe/Madrid').format('YYYY-MM-DD HH:mm ZZ') will return 2014-12-18 13:22 +0100. The Europe/Madrid indentifer should be used instead of the Etc/GMT+1 identifier.

Parse Offset

zone.parse(timestamp); // 480

Parse an offset for a timestamp constructed from Date.UTC in that zone.

This is what Moment Timezone uses to parse input into a time zone. The process is conceptually similar to the following.

Assume we want to find the exact moment of March 19 2014 8:30 am in New York. Because the offset varies between -04:00 and -05:00 in New York, we don't know what the offset was on March 19th.

Instead, we create a timestamp in UTC and pass that to zone.parse, which will return the offset at that time.

var zone ='America/New_York');
zone.parse(Date.UTC(2012, 2, 19, 8, 30)); // 240

This is the code that handles the cases referenced in the Parsing Ambiguities section above.

var zone ='America/New_York');
zone.parse(Date.UTC(2012, 2, 11, 1, 59)); // 300
zone.parse(Date.UTC(2012, 2, 11, 2, 0)); // 240

Moment Timezone uses two data formats. An unpacked version for calculations and a packed version for minified transport.

Unpacked Format


The unpacked format looks exactly like the zone object.

The data below is for Los Angeles between 2014 and 2018.

    name    : 'America/Los_Angeles',
    abbrs   : ['PST', 'PDT','PST', 'PDT', 'PST', 'PDT', 'PST', 'PDT', 'PST', 'PDT', 'PST'],
    untils  : [1394359200000, 1414918800000, 1425808800000, 1446368400000, 1457863200000, 1478422800000, 1489312800000, 1509872400000, 1520762400000, 1541322000000, null],
    offsets : [480, 420, 480, 420, 480, 420, 480, 420, 480, 420, 480]

The lengths of abbrs, untils, offsets are all the same. The offset and abbr at any index are only active while the timestamp is less than the until at that index.

An easy way to read this aloud is "between untils[n-1] and untils[n], the abbr should be abbrs[n] and the offset should be offsets[n]".

Note that untils are measured in milliseconds and offsets are measured in minutes.

Packed Format


The packed format represents an unpacked zone in a single string.

The data below is for Los Angeles between 2014 and 2018. More time zones can be seen here

'America/Los_Angeles|PST PDT|80 70|01010101010|1Lzm0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0'

In order to save as many bytes as possible, we used a very compact format to store the data.

The data is split into 5 sections separated by pipes.

1Abbr MapPST PDT
2Offset Map80 70
3Abbr/Offset Index01010101010
4Timestamp Diff1Lzm0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0

Name: The canonical name of the time zone.

Abbr Map: A space separated list of all the abbreviations ever used in this time zone.

Offset Map: A space separated list of all the offsets ever used in this time zone in minutes in base 60.

Abbr/Offset Index: A tightly packed array of indices into the offset and abbr maps. These are also in base 60.

Timestamp Diffs: This is where the timestamps are stored.

Because we are dealing with a sorted list of timestamps, we just store the diff from the last timestamps rather than storing the full timestamps.

The first item in the array is a unix timestamp in minutes. All items after the first item are numbers of minutes to be added to the previous value during unpacking. All items are stored in base 60.

As you may have seen from the example above, the timestamp diffs tend to duplicate the same values from year to year. These duplications allow gzip to compress the data even further than if we used full timestamps.

Base 60?

You may be wondering why base 60 is used. Base 62 is a fairly common tool for ascii data compression, using a-z to represent 10-35 and A-Z to represent 36-61.

While it may have saved a few bytes to use base 62, much of the data in Moment Timezone maps nicely to multiples of 60.

There are 60 minutes in an hour and 60 seconds in a minute. 3 hours is 30 minutes in base 60 and 300 seconds in base 60 instead of 180 and 10800 in base 10 or 2U and 2Oc in base 62.

Link Format


In order to reduce duplication, the Moment Timezone data packer will create links out of two zones that share data that is exactly the same.

This data is the two zone names separated by a pipe.'America/Los_Angeles|PST PDT|80 70|01010101010|1Lzm0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0');'America/Los_Angeles|US/Pacific');"2013-12-01", "America/Los_Angeles").format(); // 2013-12-01T00:00:00-08:00"2013-12-01", "US/Pacific").format();          // 2013-12-01T00:00:00-08:00

Once the data has been packed and transported to the client, it must be added to Moment Timezone.

Adding a Zone


To add zone data to Moment Timezone, use'America/Los_Angeles|PST PDT|80 70|0101|1Lzm0 1zb0 Op0');

To add more than one zone, pass an array of packed data.[
    'America/Los_Angeles|PST PDT|80 70|0101|1Lzm0 1zb0 Op0',
    'America/New_York|EST EDT|50 40|0101|1Lz50 1zb0 Op0'

Note: The above zone data is sample data and is not up to date. Reference the moment-timezone source for up to date data.

Adding a Link


To link two zone names to the same data, use

The strings passed in should be in the link format: the two zone names separated by a pipe.'America/Los_Angeles|US/Pacific');

To add more than one link at a time, pass an array of link strings.[

Loading a Data Bundle

    zones : [],
    links : [],
    version : '2014e'

The data for Moment Timezone comes from the IANA Time Zone Database. New versions are released periodically as time zone laws change in various countries.

The versions are named after the year and an incrementing letter. 2014a 2014b 2014c...

In order to keep versions together, Moment Timezone has a bundled object format as well.

    version : '2014e',
    zones : [
        'America/Los_Angeles|PST PDT|80 70|0101|1Lzm0 1zb0 Op0',
        'America/New_York|EST EDT|50 40|0101|1Lz50 1zb0 Op0'
    links : [

To load a bundle into Moment Timezone, use{
    version : '2014e',
    zones : [...],
    links : [...]

Checking Zone Existence

edit; // Zone or null

To check if a zone exists, use It will return the Zone if it was loaded and null if it was not loaded."UnloadedZone"); // null"UnloadedZone|UZ|0|0|");"UnloadedZone"); // Zone { name : "UnloadedZone", ...}

Getting Zone Names

edit; // String[]

To get a list of all available time zone names, use; // ["Africa/Abidjan", "Africa/Accra", "Africa/Addis_Ababa", ...]

Because of the complexity of the packed and unpacked data formats, Moment Timezone has some heavily tested utility functions for working with the data.

Methods for unpacking data are included with the core library, as they are needed in order to use the library.

Methods for packing and subsetting the data are included in an additional moment-timezone-utils.js file. This file adds some more methods to the namespace.

// in moment-timezone.js
// in moment-timezone-utils.js


edit; // PackedString

This converts data in the unpacked format to the packed format.

var unpacked = {
    name    : 'Indian/Mauritius',
    abbrs   : ['LMT', 'MUT', 'MUST', 'MUT', 'MUST', 'MUT'],
    offsets : [-230, -240, -300, -240, -300, -240],
    untils  : [-1988164200000, 403041600000, 417034800000, 1224972000000, 1238274000000, null]
};; // "Indian/Mauritius|LMT MUT MUST|-3O -40 -50|012121|-2xorO 34unO 14L0 12kr0 11z0"


edit; // UnpackedObject

This converts data in the packed format to the unpacked format.

var packed = "Indian/Mauritius|LMT MUT MUST|-3O -40 -50|012121|-2xorO 34unO 14L0 12kr0 11z0";;
// {
//     name    : 'Indian/Mauritius',
//     abbrs   : ['LMT', 'MUT', 'MUST', 'MUT', 'MUST', 'MUT'],
//     offsets : [-230, -240, -300, -240, -300, -240],
//     untils  : [-1988164200000, 403041600000, 417034800000, 1224972000000, 1238274000000, null]
// };

Pack Base 60

edit; // Base60String

Convert a base 10 number to a base 60 string.;    // 9;   // a;   // X; // mh

Much like Number.prototype.toFixed, accepts a second argument for the number of digits of precision.,   1); // 1.a, 3); // k.7op,       1); // X

A solitary 0 before the decimal point is dropped., 1); // 1.a, 1); // .a

Trailing zeroes after the decimal point are dropped., 1); // .a, 5); // .a, 5);  // X

Unpack Base 60

edit; // Number

Convert a base 60 string to a base 10 number.'9');     // 9'a');     // 10'X');     // 59'mh');    // 1337'1.9');   // 1.15'k.7op'); // 20.123449074074074

Create Links

edit; // UnpackedBundle

In order to reduce duplication, we can create links out of two zones that share data.

var unlinked = {
    zones : [
    links : [],
    version : "2014x-doc-example"

    zones : [
    links : ["Zone/One|Zone/Two"],
    version : "2014x-doc-example"

This is especially useful when combined with, as older rules that would have differentiated two Zones may not be in the filtered year range, allowing them to be linked to save space.

Filter Years

edit, Number, Number); // UnpackedZone

By default, Moment Timezone includes all the data from the IANA Time Zone Database. This includes data from 1900 to 2038. The data for all these years may not be necessary for your use case. can be used to filter out data for years outside a certain range.

var all    = { name : "America/Los_Angeles", abbrs : [...], offsets : [...] untils : [...]};
var subset =, 2012, 2016);
all.untils.length;    // 186
subset.untils.length; // 11

If only one year is passed, it will be used for the start and end year.

var all    = { name : "America/Los_Angeles", abbrs : [...], offsets : [...] untils : [...]};
var subset =, 2012);
all.untils.length;    // 186
subset.untils.length; // 3

Filter Years, Create Links, and Pack

edit, Number, Number); // PackedBundle

The packing, link creation, and subsetting of years are all tools for compressing data to be transported to the client.

The method combines all these into one simple interface. Pass in an unpacked bundle, start year, and end year and get a filtered, linked, packed bundle back.

This is what is being used to compress the output for the bundled data + library files on the homepage.