momentJS:从其他时区中的日期获取错误的时间戳 [英] momentJS: Getting wrong timestamp from date in a different timezone
问题描述
只有一次输入,即开始时间
There is one time input i.e. start_time
我正在尝试获取这些输入的时间戳(以毫秒为单位)
I am trying to get timestamp in milliseconds for these inputs
let start_time = "17:05:00";
var start_date_moment = moment(start_time, "HH:mm:ss");
console.log(start_timestamp);
output is -> moment("2019-04-24T17:05:00.000")
此输出在服务器和本地上均相同
This output remains same on server and local
但是当我尝试以相同的方式获取以毫秒为单位的unix时间戳
But when I am trying to get unix timestamp in milliseconds in the same way
var start_timestamp = moment(start_time, "HH:mm:ss").valueOf();
在不同时区的服务器上
console.log(start_timestamp);//1556125500000
console.log(moment(start_timestamp/1000).format('YYYY-MM-DD HH:mm:ss'); //2019-04-24 17:05:00
在本地
console.log(start_timestamp);//1556105700000
console.log(moment(start_timestamp/1000).format('YYYY-MM-DD HH:mm:ss'); //2019-04-24 22:35:00
此start_timestamp值在本地和服务器上不同.但是时间戳不应随时区而变化,所有时区都应保持不变.请帮助我.
This start_timestamp value is different on local and server. But timestamp shouldn't change with timezone, it should remains same for all timezones. Please help me with this.
如何在两个地方都获得正确且相同的值.我得到了与此链接相关的一些 https://github.com/moment/moment/issues /2035
How to get the correct and same value at both places. I got this link some what related to this https://github.com/moment/moment/issues/2035
日期和任何特定格式都没有问题,只有时间戳才有问题.
There is no issue with dates any particular format, issue is only with timestamp.
推荐答案
You need to take the offset into consideration when using moment (using timezones moment.js). Since no offset was passed in the input, the moment will be based on the time zone of the computer the code is running on, hence the different values..
示例:
var a = moment.tz("2013-11-18 11:55", "Asia/Taipei");
var b = moment.tz("2013-11-18 11:55", "America/Toronto");
a.format(); // 2013-11-18T11:55:00+08:00
b.format(); // 2013-11-18T11:55:00-05:00
a.utc().format(); // 2013-11-18T03:55Z
b.utc().format(); // 2013-11-18T16:55Z
如果使用moment-timezone更改moment
对象的时区,则只会影响本地时间的值.它不会更改所表示的时刻,因此也不会更改基础时间戳.
If you change the time zone of a moment
object using moment-timezone only affects the value of the local time. It does not change the moment in time being represented, and therefore does not change the underlying timestamp.
Unix时间戳始终基于UTC -您可以在世界上任何给定位置将其视为相同的时间戳.
A Unix Timestamp is always based on UTC - you can see it as the same timestamp at any given location in the world.
如果使用utcOffset
,则应传递一个整数:
If you use utcOffset
you should pass an integer:
示例:
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"
MomentJS允许将偏移量参数作为字符串传递,但是它期望字符串采用ISO8601格式之一:[+/-]HH:mm
或[+/-]HHmm
.
MomentJS allows offset-arguments to be passed as string, but it expects the string to be in one of the ISO8601 formats: [+/-]HH:mm
or [+/-]HHmm
.
要避免全部发生,您可以(如果知道)将位置作为参数传递,例如
To avoid this all together you could, if known, pass the location as an argument like
moment.tz(start_time, "HH:mm:ss", "Asia/Kolkata").valueOf();
如上面的第一个示例所述.
as mentioned in the first example above..
这篇关于momentJS:从其他时区中的日期获取错误的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!