Javascript 从 moment.js 对象中删除时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34050389/
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 15:51:58  来源:igfitidea点击:

Remove timezone from a moment.js object

javascriptdatetimepickermomentjs

提问by Alvaro

I'm using datetimepicker.jsand its datefunction returns a moment.jsobject. It does so with the local UTC offset in it and my original date has a different offset.

我正在使用datetimepicker.js并且它的date函数返回一个moment.js对象。它使用本地 UTC 偏移量这样做,而我的原始日期具有不同的偏移量。

My original date:

我的原始日期:

2015-10-01T15:00:00.000Z

What I display on the date time picker (DD-MM HH:mm):

我在日期时间选择器 ( DD-MM HH:mm)上显示的内容:

01-10 15:00

What I get:

我得到的:

2015-10-01T15:40:00+01:00

What I want:

我想要的是:

2015-10-01T15:40:00+00:00

Note how I removed the +01 offset at the end.

请注意我最后是如何删除 +01 偏移量的。

How can I do this applying it for any local UTC ? This is, without having to manually remove the 01 (as it can be a any other local offset depending on the user location).

我怎样才能做到这一点,将它应用于任何本地 UTC ?这是,无需手动删除 01(因为它可以是任何其他本地偏移,具体取决于用户位置)。

var momentDate = timePicker.data("DateTimePicker").date();
console.log(momentDate.format());
//this prints  2015-10-01T15:40:00+01:00

回答by Matt

You need to explicitly specify the format.

您需要明确指定格式。

Try this:

尝试这个:

momentDate.format('YYYY-MM-DDTHH:mm:ss')

this will give the result as

这将给出结果为

2015-10-01T15:40:00

2015-10-01T15:40:00

回答by Matt Johnson-Pint

It sounds like you are trying to have the user pick a UTC-based date and time. Therefore, the best way would be to have the picker operate in UTC mode when it creates the moment to begin with. I'm not familiar with this particular datetimepicker, but assuming somewhere internally it does something like this:

听起来您正试图让用户选择基于 UTC 的日期和时间。因此,最好的方法是让选择器在创建开始时刻时以 UTC 模式运行。我不熟悉这个特定的日期时间选择器,但假设在内部某个地方它会做这样的事情:

var m = moment([year, month-1, day, hour, minute]);

Then is should instead do this:

然后是应该这样做:

var m = moment.utc([year, month-1, day, hour, minute]);

(The variables shown here would be coming from within the picker control.)

(此处显示的变量将来自选取器控件内。)

Ideally, the picker control should include a feature to set UTC mode so it can do this internally, when told to by you.

理想情况下,选择器控件应包含设置 UTC 模式的功能,以便在您告知时可以在内部执行此操作。

If it doesn't have such a feature, you can still compensate yourself. Unfortunately, you can't just call .utc(), as that would give a different time than the one the user picked. So, you'll have to compensate by shifting the UTC time by the original moment's offset.

如果它没有这样的功能,你仍然可以补偿自己。不幸的是,您不能只调用.utc(),因为这会给出与用户选择的时间不同的时间。因此,您必须通过将 UTC 时间偏移原始时刻的偏移量来进行补偿。

var m = // moment value from the picker
var result = moment(m).utc().add(m.utcOffset(), 'm');

You can then call formator whatever you wish on the result. Notice that the original moment is clonedwith moment(m), such that the offset doesn't get lost and the switch to UTC doesn't interfere with the picker's internal behavior.

然后,您可以format根据结果调用或进行任何您希望的操作。请注意,原始时刻是用克隆moment(m),这样偏移量就不会丢失,并且切换到 UTC 不会干扰选择器的内部行为。

Also, note that shiftinglike this is generally a hack, and if done wrong can lead to errors. Here it's ok, because the moment is already in UTC mode when the adjustment is applied. But as a general solution, shifting should be avoided. The best option is to have the control placed into UTC mode to begin with.

另外,请注意,像这样的移动通常是一种黑客行为,如果做错了可能会导致错误。在这里就可以了,因为在应用调整时,时刻已经处于 UTC 模式。但作为一般解决方案,应避免移位。最好的选择是首先将控件置于 UTC 模式。

回答by Frofike

Try this:

尝试这个:

let str = '2015-10-01T15:40+01:00';
let moment = moment(str).utcOffset(str)
console.log(moment.format('DD/MM/YYYY HH:mm'))
<script src="https://momentjs.com/downloads/moment.js"></script>

回答by Wep0n

This worked for me:

这对我有用:

let date = "2017-02-01 15:20:00.00";
let pattern = "YYYY-MM-DD HH:mm:ss.SS"
let d = moment(date, pattern).utc(false);

It will still say that it considered the timezone, but I find that it doesn't.

它仍然会说它考虑了时区,但我发现它没有。

LOG: 'ParseDateStr::2017-2-1 15:20:00.0000000
ParsedDate::Wed Feb 01 2017 15:20:00 GMT+0100 (Central European Standard Time)'

Normally, with just moment(date, pattern)it would parse as 16:20:00

通常,只要moment(date, pattern)它会解析为16:20:00

回答by Craig

Not a clean solution but you could do

不是一个干净的解决方案,但你可以做

momentDate.format('YYYY-MM-DDTHH:mm:ss') + '00:00';

This would remove the timezone and then append the '00:00' part back in. This is to extend Matt's answer.

这将删除时区,然后重新添加 '00:00' 部分。这是为了扩展马特的答案。

Which would give you

哪个会给你

2015-10-01T15:40:00+00:00

2015-10-01T15:40:00+00:00

回答by Darlan Dieterich

most simple possible:

最简单的可能:

let str = "2018-03-20T16:29:35-03:00";
let res = str.substring(0, 19); //2018-03-20T16:29:35
let hi = moment(res).format('LLL')
console.log(hi) //20 de Mar?o de 2018 às 16:29