utcOffset()


moment().utcOffset();
moment().utcOffset(Number|String);
moment().utcOffset(Number|String, Boolean);

获取 UTC 偏移量(以分钟为单位)。

注意:与 moment.fn.zone 不同,此函数返回 UTC 的实际偏移量,而不是反向偏移量(类似 Date.prototype.getTimezoneOffset 返回的)。

获取当前对象的 utcOffset

moment().utcOffset(); // (-240、-120、-60、0、60、120、240 等)

通过提供分钟数来设置 UTC 偏移量。 在调用 utcOffset() 的 moment 对象上设置偏移量。 如果想要全局地设置偏移量,则尝试使用 moment-timezone。 注意,一旦设置了偏移量,则它便会固定且不会单独更改(即没有 DST 规则)。 如果想要一个实际的时区(特定位置的时间),例如 America/Los_Angeles,则考虑使用 moment-timezone

moment().utcOffset(120);

如果输入小于 16 且大于 -16,则会将输入解释为小时。

// 这些是等效的。
moment().utcOffset(8);  // 设置小时偏移
moment().utcOffset(480);  // 设置分钟偏移 (8 * 60)

也可以设置字符串的 UTC 偏移量。

// 这些是等效的。
moment().utcOffset("+08:00");
moment().utcOffset(8);
moment().utcOffset(480);

moment#utcOffset 会在字符串中搜索 +00:00 +0000 -00:00 -0000 Z 的第一个匹配项,因此甚至可以传入 ISO8601 格式的字符串,且 moment 将会更改为 UTC 偏移量。

注意,如果字符串不是 'Z',则必须以 +- 字符开头。

moment().utcOffset("2013-03-07T07:00:00+08:00");

utcOffset 函数具有可选的第二个参数,该参数接受一个布尔值,该布尔值表明是否保留日期中的现有时间。

  • 传入 false(默认)将会在世界标准时间中保持不变,但本地时间将会改变。

  • 传入 true 将保留相同的本地时间,但要以在世界标准时间中选择其他时间为代价。

此特性的一种用法是,如果只想使用数字型输入值来构造具有特定时区偏移量的 moment:

moment([2016, 0, 1, 0, 0, 0]).utcOffset(-5, true) // 等效于 "2016-01-01T00:00:00-05:00"
moment().utcOffset();
moment().utcOffset(Number|String);
moment().utcOffset(Number|String, Boolean);

Get the UTC offset in minutes.

Note: Unlike moment.fn.zone this function returns the real offset from UTC, not the reverse offset (as returned by Date.prototype.getTimezoneOffset).

Getting the utcOffset of the current object:

moment().utcOffset(); // (-240, -120, -60, 0, 60, 120, 240, etc.)

Setting the UTC offset by supplying minutes. The offset is set on the moment object that utcOffset() is called on. If you are wanting to set the offset globally, try using moment-timezone. Note that once you set an offset, it's fixed and won't change on its own (i.e there are no DST rules). If you want an actual time zone -- time in a particular location, like America/Los_Angeles, consider moment-timezone.

moment().utcOffset(120);

If the input is less than 16 and greater than -16, it will interpret your input as hours instead.

// these are equivalent
moment().utcOffset(8);  // set hours offset
moment().utcOffset(480);  // set minutes offset (8 * 60)

It is also possible to set the UTC offset from a string.

// these are equivalent
moment().utcOffset("+08:00");
moment().utcOffset(8);
moment().utcOffset(480);

moment#utcOffset will search the string for the first match of +00:00 +0000 -00:00 -0000 Z, so you can even pass an ISO8601 formatted string and the moment will be changed to that UTC offset.

Note that the string, if not 'Z', is required to start with the + or - character.

moment().utcOffset("2013-03-07T07:00:00+08:00");

The utcOffset function has an optional second parameter which accepts a boolean value indicating whether to keep the existing time of day.

  • Passing false (the default) will keep the same instant in Universal Time, but the local time will change.

  • Passing true will keep the same local time, but at the expense of choosing a different point in Universal Time.

One use of this feature is if you want to construct a moment with a specific time zone offset using only numeric input values:

moment([2016, 0, 1, 0, 0, 0]).utcOffset(-5, true) // Equivalent to "2016-01-01T00:00:00-05:00"