用moment()更新时间偏移量.utcOffset() [英] Updating time offset with moment().utcOffset()
问题描述
我在尝试使用 moment.js
处理时间偏移时遇到了一些问题。
I'm facing some issue trying to use moment.js
for dealing with time offsets.
I在隐藏的输入中收集本地用户时间偏移:
I collect in an hidden input the local user time offset:
<script type="text/javascript">
$(document).ready(function () {
$('input#timeoffset').val(moment().utcOffset());
});
</script>
偏移量得到正确存储(在我的情况下,其值为-240)。稍后在服务器端(在utc时间运行)我尝试更新一些db存储的utcDate做类似的事情:
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:
var userDate = moment(utcDate).utcOffset(offset)
我的问题如下:
if我按上述方式运行我的代码我没有效果:
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?
在客户端,我在服务器端使用moment.js v2.10.6 moment-timezone.js v0.4.0和moment.js v2.10.6
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
推荐答案
主要问题是您将偏移量作为字符串而不是数字传递。
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允许将偏移量作为字符串传递,但它希望它们采用ISO8601格式之一: [+/-] HH:mm
或 [+/-] HHmm
。
Moment does allow for offsets to be passed as strings, but it expects them to be in one of the ISO8601 formats: either [+/-]HH:mm
or [+/-]HHmm
.
moment.utc("2015-10-01 01:24:21").utcOffset("-04:00").format('YYYYMMDD HHmmss ZZ')
// "20150930 212421 -0400"
另外,请注意我使用 moment.utc(...)
解析输入字符串。您刚刚使用时刻(...)
,这将使用本地时区,除非时区是明确的或者您传递日期
对象而不是字符串。它还会在本地模式中保留时刻
对象,因此除非时区,否则您的 utcDate
输出将是错误的该机器的实际设置为UTC。
Additionally, note that I used moment.utc(...)
to parse the input string. You just used moment(...)
which will use the local time zone unless the time zone is explicit or if you pass a Date
object instead of a string. It will also leave the moment
object in "local mode", so your utcDate
output would be wrong unless the time zone of the machine was actually set to UTC.
最后,不要忘记时区!=偏移。您不能假设您获得的偏移对所有日期都有效。如果您需要将日期投影到用户的时区,您必须实际知道时区,例如 America / New_York
。你可以将它们与moment-timezone插件一起使用。
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.
这篇关于用moment()更新时间偏移量.utcOffset()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!