Javascript 如何将 Moment.js 日期转换为用户本地时区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29220467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:00:22  来源:igfitidea点击:

How to convert Moment.js date to users local timezone?

javascriptdatetimezonemomentjs

提问by oliver

I use the Moment.js and Moment-Timezone frameworks, and have a Moment.js date object which is explicitly in UTC timezone. How can I convert that to the current timezone of the browser?

我使用 Moment.js 和 Moment-Timezone 框架,并有一个 Moment.js 日期对象,该对象明确位于 UTC 时区。如何将其转换为浏览器的当前时区?

var testDateUtc = moment.tz("2015-01-30 10:00:00", "UTC"); var localDate = ???

var testDateUtc = moment.tz("2015-01-30 10:00:00", "UTC"); var localDate = ???

So it would be fine if I could find out the users local time zone; or alternatively I'd like to convert the date object into another data object which just uses the "local timezone", no matter what that actually is.

所以如果我能找出用户的本地时区就好了;或者,我想将日期对象转换为另一个只使用“本地时区”的数据对象,无论实际是什么。

回答by Matt Johnson-Pint

You do not need to use moment-timezone for this. The main moment.js library has full functionality for working with UTC and the local time zone.

您不需要为此使用时刻时区。主要的 moment.js 库具有使用 UTC 和本地时区的完整功能。

var testDateUtc = moment.utc("2015-01-30 10:00:00");
var localDate = moment(testDateUtc).local();

From there you can use any of the functions you might expect:

从那里您可以使用您可能期望的任何功能:

var s = localDate.format("YYYY-MM-DD HH:mm:ss");
var d = localDate.toDate();
// etc...

Note that by passing testDateUtc, which is a momentobject, back into the moment()constructor, it creates a clone. Otherwise, when you called .local(), it would also change the testDateUtcvalue, instead of just the localDatevalue. Moments are mutable.

请注意,通过将testDateUtc一个moment对象传递回moment()构造函数,它会创建一个clone。否则,当您调用 时.local(),它也会更改testDateUtc值,而不仅仅是localDate值。时刻是可变的

Also note that if your original input contains a time zone offset such as +00:00or Z, then you can just parse it directly with moment. You don't need to use .utcor .local. For example:

另请注意,如果您的原始输入包含时区偏移量,例如+00:00Z,那么您可以直接使用moment. 您不需要使用.utc.local。例如:

var localDate = moment("2015-01-30T10:00:00Z");

回答by AndrewHenderson

var dateFormat = 'YYYY-DD-MM HH:mm:ss';
var testDateUtc = moment.utc('2015-01-30 10:00:00');
var localDate = testDateUtc.local();
console.log(localDate.format(dateFormat)); // 2015-30-01 02:00:00
  1. Define your date format.
  2. Create a moment object and set the UTC flag to true on the object.
  3. Create a localized moment object converted from the original moment object.
  4. Return a formatted string from the localized moment object.
  1. 定义日期格式。
  2. 创建一个 moment 对象并将对象上的 UTC 标志设置为 true。
  3. 创建从原始力矩对象转换而来的局部力矩对象。
  4. 从本地化的矩对象返回格式化的字符串。

See: http://momentjs.com/docs/#/manipulating/local/

请参阅:http: //momentjs.com/docs/#/manipulating/local/

回答by sayan

Here's what I did:

这是我所做的:

var timestamp = moment.unix({{ time }});
var utcOffset = moment().utcOffset();
var local_time = timestamp.add(utcOffset, "minutes");
var dateString = local_time.fromNow();

Where {{ time }}is the utc timestamp.

{{ time }}UTC 时间戳在哪里。

回答by Alexey Ryazhskikh

Use utcOffset function.

使用 utcOffset 函数。

var testDateUtc = moment.utc("2015-01-30 10:00:00");
var localDate = moment(testDateUtc).utcOffset(10 * 60); //set timezone offset in minutes
console.log(localDate.format()); //2015-01-30T20:00:00+10:00