Moment.js timezone valueOf返回错误的时间戳 [英] Moment.js timezone valueOf returning wrong timestamp

查看:195
本文介绍了Moment.js timezone valueOf返回错误的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用moment.js将输入时刻转移到不同的时区并获取其时间戳。

I want to use moment.js to shift an input moment to a different timezone and get its timestamp.

moment.tz(moment(), "Pacific/Auckland").valueOf();

问题是,当我这样做时, moment.tz( )对象看起来不错,但 valueOf()方法以某种方式计算回到我的计算机上设置的时区。

The problem is, while I am doing this, the moment.tz() object is looking good, but the valueOf() method is somehow calculating this back to timezone which is set on my computer.

我的方法出了什么问题?

What is wrong with my approach?

非常感谢。

编辑1

moment.tz(moment(),Pacific / Auckland)。format() ;
给我正确的时间字符串

moment.tz(moment(), "Pacific/Auckland").format(); is giving me the right time string

moment.tz(moment(),Pacific / Auckland)。valueOf(x);
给我毫秒,但是在我当地的时间再没有太平洋/奥克兰时间

moment.tz(moment(), "Pacific/Auckland").valueOf("x"); is giving me the milliseconds, but again in my local time and not in "Pacific/Auckland" time

所以毫秒计算毫秒将其转换回当地时间,但为什么呢?什么是正确的方法来获得预期时区的毫秒?

So somehow the calculation of milliseconds is converting it back to the local time, but why? And whats the correct approach to get the milliseconds in the expected timezone?

推荐答案

我认为可能只是概念上缺乏理解时间戳实际上是什么。

I think there might just be a conceptual lack of understanding of what the timestamp actually is.

假设我现在的时间是:

04/25 / 2016 @ 10:21 am(UTC-7)

04/25/2016 @ 10:21am (UTC-7)

这一刻的时间戳是什么?

What is the timestamp of this moment?

1461604867

1461604867

什么是UTC时间?

04/25/2016 @ 5:21 pm(UTC)

04/25/2016 @ 5:21pm (UTC)

这一刻的时间戳是什么?

What is the timestamp of this moment?

1461604867

什么,同样,为什么?

时刻是相同的时间戳,无论它在哪个时区当你召唤时刻()时,它指的是现在正确的,这个特殊的时刻在盛大的时空连续体中。与Date.now()相同。实际上,你读到这一刻的那一刻,仍然是你,我在哪里,在日本,在南极洲,在叙利亚,在格陵兰岛等地的完全相同的时刻。恰巧这一刻在视觉上得到了表现(时间字符串)人类在每个区域以不同的方式。

A moment in time is the same timestamp, regardless of which timezone it is in. When you call moment(), it is referring to right NOW, this particular moment in time in the grand space-time continuum. Same with Date.now(). The moment you are reading this is, in reality, still the exact same moment where you are, where I am, in Japan, in Antarctica, in Syria, in Greenland, etc. It just so happens that this one moment is visually represented (the time string) by humans in different ways in each area.

时刻时区不会影响它存储的实际Date对象。它只影响日期的视觉表示。视觉表示随时区变化。这就是为什么你不会得到不同的时间戳。

Moment timezone does not affect the actual Date object it stores. It only affects the visual representation of the date. The visual representation varies across timezone. That is why you will not get a different timestamp.

这篇关于Moment.js timezone valueOf返回错误的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆