Javascript 使用 moment().utcOffset() 更新时间偏移

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

Updating time offset with moment().utcOffset()

javascriptexpresstimemomentjsutc

提问by Pierluigi

I'm facing some issue trying to use moment.jsfor dealing with time offsets.

我在尝试moment.js用于处理时间偏移时遇到了一些问题。

I collect in an hidden input the local user time offset:

我在隐藏输入中收集本地用户时间偏移:

<script type="text/javascript">
  $(document).ready(function () { 
    $('input#timeoffset').val(moment().utcOffset()); 
  });
</script>

The offset gets correctly stored (in my case its value is -240). Later on the server side (which runs in utc time) I try to update some db stored utcDate doing something like:

偏移量被正确存储(在我的例子中它的值为 -240)。稍后在服务器端(以 utc 时间运行),我尝试更新一些存储的 utcDate db 执行以下操作:

var userDate = moment(utcDate).utcOffset(offset)

My issue is the following: if I run my code as above described I get no effects:

我的问题如下:如果我按照上述方式运行代码,则没有任何效果:

  • utcDate: 20151001 012421 +0000
  • userDate: 20151001 012421 +0000
  • utcDate: 20151001 012421 +0000
  • userDate: 20151001 012421 +0000

If I flip the offset sign I get:

如果我翻转偏移符号,我会得到:

  • utcDate: 20151001 012421 +0000
  • userDate: 20151001 052421 +0400
  • utcDate: 20151001 012421 +0000
  • userDate: 20151001 052421 +0400

I'm clearly doing something wrong (even if my expectation was that the first version was correct), do you have any hint?

我显然做错了什么(即使我的期望是第一个版本是正确的),你有什么提示吗?

On client-side I'm using moment.js v2.10.6 while on the server-side moment-timezone.js v0.4.0 and moment.js v2.10.6

在客户端我使用 moment.js v2.10.6 而在服务器端 moment-timezone.js v0.4.0 和 moment.js v2.10.6

回答by Matt Johnson-Pint

The main issue is that you are passing the offset as a string instead of a number.

主要问题是您将偏移量作为字符串而不是数字传递。

moment.utc("2015-10-01 01:24:21").utcOffset("-240").format('YYYYMMDD HHmmss ZZ')
// "20151001 012421 +0000"

moment.utc("2015-10-01 01:24:21").utcOffset(-240).format('YYYYMMDD HHmmss ZZ')
// "20150930 212421 -0400"

When you have an offset in terms of minutes, then you must use the numeric form. You can always convert it:

当您有以分钟为单位的偏移量时,您必须使用数字形式。您可以随时转换它:

moment.utc("2015-10-01 01:24:21").utcOffset(+"-240").format('YYYYMMDD HHmmss ZZ')
// "20150930 212421 -0400"

Moment does allow for offsets to be passed as strings, but it expects them to be in one of the ISO8601 formats: either [+/-]HH:mmor [+/-]HHmm.

那一刻确实允许偏差为字符串传递,但预计他们在的ISO8601格式之一:要么[+/-]HH:mm[+/-]HHmm

moment.utc("2015-10-01 01:24:21").utcOffset("-04:00").format('YYYYMMDD HHmmss ZZ')
// "20150930 212421 -0400"

Additionally, note that I used moment.utc(...)to parse the input string. You just used moment(...)which will use the localtime zone unless the time zone is explicit or if you pass a Dateobject instead of a string. It will also leave the momentobject in "local mode", so your utcDateoutput would be wrong unless the time zone of the machine was actually set to UTC.

另外,请注意我曾经moment.utc(...)解析输入字符串。您刚刚使用moment(...)which 将使用本地时区,除非时区是明确的,或者您传递的是Date对象而不是字符串。它还将使moment对象处于“本地模式”,因此utcDate除非机器的时区实际上设置为 UTC,否则您的输出将是错误的。

Lastly, don't forget "Time Zone != Offset". You can't assume that the offset you obtained is valid for all dates. If you need to project a date to to the user's time zone, you have to actually know the time zone, such as America/New_York. You can use these with the moment-timezone plugin.

最后,不要忘记“时区!= 偏移”。您不能假设您获得的偏移量对所有日期都有效。如果需要将日期投影到用户的时区,则必须实际知道时区,例如America/New_York. 您可以将它们与 moment-timezone 插件一起使用。