使用moment.js将日期转换为UTC

时间:2022-08-25 10:48:13

Probably and easy answer to this but I can't seem to find a way to get moment.js to return a UTC date time in milliseconds. Here is what I am doing:

可能很容易回答这个但我似乎找不到让moment.js以毫秒为单位返回UTC日期时间的方法。这是我在做的事情:

var date = $("#txt-date").val(),
    expires = moment.utc(date);

Any idea what I am doing wrong?

知道我做错了什么吗?

5 个解决方案

#1


51  

This is found in the documentation. With a library like moment, I urge you to read the entirety of the documentation. It's really important.

这可以在文档中找到。有了类似于时刻的图书馆,我恳请您阅读完整的文档。这非常重要。

Assuming the input text is entered in terms of the users's local time:

假设输入文本是根据用户的本地时间输入的:

 var expires = moment(date).valueOf();

If the user is instructed actually enter a UTC date/time, then:

如果指示用户实际输入UTC日期/时间,则:

 var expires = moment.utc(date).valueOf();

#2


18  

I use this method and works. ValueOf not works to me.

我使用这种方法并且有效。 ValueOf对我不起作用。

moment.utc(yourDate).format()

#3


1  

If all else fails, just reinitialize with an inverse of your local offset.

如果所有其他方法都失败了,只需使用本地偏移量的倒数重新初始化即可。

var timestamp = new Date();
var inverseOffset = moment(timestamp).utcOffset() * -1;
timestamp = moment().utcOffset( inverseOffset  );

timestamp.toISOString(); // This should give you the accurate UTC equivalent.

#4


1  

moment.utc(date).format(...); 

is the way to go, since

是这样的,因为

moment().utc(date).format(...);

does behave weird...

确实表现得很奇怪......

#5


0  

Don't you need something to compare and then retrieve the milliseconds?

你需要比较一些东西,然后检索毫秒?

For instance:

let enteredDate = $("#txt-date").val(); // get the date entered in the input
let expires = moment.utc(enteredDate); // convert it into UTC

With that you have the expiring date in UTC. Now you can get the "right-now" date in UTC and compare:

有了这个,你有UTC的到期日期。现在您可以获得UTC中的“正确”日期并进行比较:

var rightNowUTC = moment.utc(); // get this moment in UTC based on browser
let duration = moment.duration(rightNowUTC.diff(expires)); // get the diff
let remainingTimeInMls = duration.asMilliseconds();

#1


51  

This is found in the documentation. With a library like moment, I urge you to read the entirety of the documentation. It's really important.

这可以在文档中找到。有了类似于时刻的图书馆,我恳请您阅读完整的文档。这非常重要。

Assuming the input text is entered in terms of the users's local time:

假设输入文本是根据用户的本地时间输入的:

 var expires = moment(date).valueOf();

If the user is instructed actually enter a UTC date/time, then:

如果指示用户实际输入UTC日期/时间,则:

 var expires = moment.utc(date).valueOf();

#2


18  

I use this method and works. ValueOf not works to me.

我使用这种方法并且有效。 ValueOf对我不起作用。

moment.utc(yourDate).format()

#3


1  

If all else fails, just reinitialize with an inverse of your local offset.

如果所有其他方法都失败了,只需使用本地偏移量的倒数重新初始化即可。

var timestamp = new Date();
var inverseOffset = moment(timestamp).utcOffset() * -1;
timestamp = moment().utcOffset( inverseOffset  );

timestamp.toISOString(); // This should give you the accurate UTC equivalent.

#4


1  

moment.utc(date).format(...); 

is the way to go, since

是这样的,因为

moment().utc(date).format(...);

does behave weird...

确实表现得很奇怪......

#5


0  

Don't you need something to compare and then retrieve the milliseconds?

你需要比较一些东西,然后检索毫秒?

For instance:

let enteredDate = $("#txt-date").val(); // get the date entered in the input
let expires = moment.utc(enteredDate); // convert it into UTC

With that you have the expiring date in UTC. Now you can get the "right-now" date in UTC and compare:

有了这个,你有UTC的到期日期。现在您可以获得UTC中的“正确”日期并进行比较:

var rightNowUTC = moment.utc(); // get this moment in UTC based on browser
let duration = moment.duration(rightNowUTC.diff(expires)); // get the diff
let remainingTimeInMls = duration.asMilliseconds();