使用momentjs将日期转换为纪元然后再回到日期

时间:2021-08-11 21:30:17

I'm trying to convert a date string to epoch, then epoch back to the date string to verify that I'm providing the correct date string.

我正在尝试将日期字符串转换为epoch,然后将epoch转换回日期字符串以验证我是否提供了正确的日期字符串。

var epoch = moment("10/15/2014 9:00").unix(); // do I need to do .local()?
var momentDate = moment(epoch); // I've also tried moment.utc(epoch) 
var momentDateStr = momentDate.calendar();
alert("Values are: epoch = " + epoch + ", momentDateStr = " + momentDateStr);

Renders

Values are: epoch = 1413378000, momentDateStr = 01/17/1970

Note: I'm using the following version of the moment js script, //cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.3/moment-with-locales.js

注意:我正在使用以下版本的时刻js脚本,//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.3/moment-with-locales.js

2 个解决方案

#1


96  

There are a few things wrong here:

这里有一些问题:

  • First, terminology. "Epoch" refers to the starting point of something. The "Unix Epoch" is Midnight, January 1st 1970 UTC. You can't convert an arbitrary "date string to epoch". You probably meant "Unix Time", which is often erroneously called "Epoch Time".

    首先,术语。 “*”指的是某事的起点。 “Unix Epoch”是UTC 1970年1月1日午夜。您无法将任意“日期字符串转换为纪元”。你可能意味着“Unix时间”,这通常被错误地称为“*时间”。

  • .unix() returns Unix Time in whole seconds, but the default moment constructor accepts a timestamp in milliseconds. You should instead use .valueOf() to return milliseconds. Note that calling .unix()*1000 would also work, but it would result in a loss of precision.

    .unix()以秒为单位返回Unix时间,但默认时刻构造函数接受以毫秒为单位的时间戳。您应该使用.valueOf()来返回毫秒。请注意,调用.unix()* 1000也可以,但会导致精度损失。

  • You're parsing a string without providing a format specifier. That isn't a good idea, as values like 1/2/2014 could be interpreted as either February 1st or as January 2nd, depending on the locale of where the code is running. (This is also why you get the deprecation warning in the console.) Instead, provide a format string that matches the expected input, such as:

    您正在解析字符串而不提供格式说明符。这不是一个好主意,因为像1/2/2014这样的值可以解释为2月1日或1月2日,具体取决于代码运行位置的区域设置。 (这也是您在控制台中获得弃用警告的原因。)而是提供与预期输入匹配的格式字符串,例如:

    moment("10/15/2014 9:00", "M/D/YYYY H:mm")
    
  • .calendar() has a very specific use. If you are near to the date, it will return a value like "Today 9:00 AM". If that's not what you expected, you should use the .format() function instead. Again, you may want to pass a format specifier.

    .calendar()有一个非常具体的用途。如果您接近日期,它将返回“今天上午9:00”之类的值。如果这不是您的预期,则应使用.format()函数。同样,您可能想要传递格式说明符。

  • To answer your questions in comments, No - you don't need to call .local() or .utc().

    要在评论中回答您的问题,否 - 您无需调用.local()或.utc()。

Putting it all together:

把它们放在一起:

var ts = moment("10/15/2014 9:00", "M/D/YYYY H:mm").valueOf();
var m = moment(ts);
var s = m.format("M/D/YYYY H:mm");
alert("Values are: ts = " + ts + ", s = " + s);

On my machine, in the US Pacific time zone, it results in:

在我的机器上,在美国太平洋时区,它导致:

Values are: ts = 1413388800000, s = 10/15/2014 9:00

值为:ts = 1413388800000,s = 10/15/2014 9:00

Since the input value is interpreted in terms of local time, you will get a different value for ts if you are in a different time zone.

由于输入值是根据本地时间解释的,因此如果您位于不同的时区,则会获得不同的ts值。

Also note that if you really do want to work with whole seconds (possibly losing precision), moment has methods for that as well. You would use .unix() to return the timestamp in whole seconds, and moment.unix(ts) to parse it back to a moment.

另请注意,如果您确实希望使用整秒(可能会丢失精度),那么也有方法。您可以使用.unix()以整秒返回时间戳,并使用moment.unix(ts)将其解析为片刻。

var ts = moment("10/15/2014 9:00", "M/D/YYYY H:mm").unix();
var m = moment.unix(ts);

#2


6  

http://momentjs.com/docs/#/displaying/unix-timestamp/

You get the number of unix seconds, not milliseconds!

你得到unix秒的数量,而不是毫秒!

You you need to multiply it with 1000 or using valueOf() and don't forget to use a formatter, since you are using a non ISO 8601 format. And if you forget to pass the formatter, the date will be parsed in the UTC timezone or as an invalid date.

您需要将它乘以1000或使用valueOf()并且不要忘记使用格式化程序,因为您使用的是非ISO 8601格式。如果您忘记传递格式化程序,则日期将在UTC时区中解析或作为无效日期进行解析。

moment("10/15/2014 9:00", "MM/DD/YYYY HH:mm").valueOf()

#1


96  

There are a few things wrong here:

这里有一些问题:

  • First, terminology. "Epoch" refers to the starting point of something. The "Unix Epoch" is Midnight, January 1st 1970 UTC. You can't convert an arbitrary "date string to epoch". You probably meant "Unix Time", which is often erroneously called "Epoch Time".

    首先,术语。 “*”指的是某事的起点。 “Unix Epoch”是UTC 1970年1月1日午夜。您无法将任意“日期字符串转换为纪元”。你可能意味着“Unix时间”,这通常被错误地称为“*时间”。

  • .unix() returns Unix Time in whole seconds, but the default moment constructor accepts a timestamp in milliseconds. You should instead use .valueOf() to return milliseconds. Note that calling .unix()*1000 would also work, but it would result in a loss of precision.

    .unix()以秒为单位返回Unix时间,但默认时刻构造函数接受以毫秒为单位的时间戳。您应该使用.valueOf()来返回毫秒。请注意,调用.unix()* 1000也可以,但会导致精度损失。

  • You're parsing a string without providing a format specifier. That isn't a good idea, as values like 1/2/2014 could be interpreted as either February 1st or as January 2nd, depending on the locale of where the code is running. (This is also why you get the deprecation warning in the console.) Instead, provide a format string that matches the expected input, such as:

    您正在解析字符串而不提供格式说明符。这不是一个好主意,因为像1/2/2014这样的值可以解释为2月1日或1月2日,具体取决于代码运行位置的区域设置。 (这也是您在控制台中获得弃用警告的原因。)而是提供与预期输入匹配的格式字符串,例如:

    moment("10/15/2014 9:00", "M/D/YYYY H:mm")
    
  • .calendar() has a very specific use. If you are near to the date, it will return a value like "Today 9:00 AM". If that's not what you expected, you should use the .format() function instead. Again, you may want to pass a format specifier.

    .calendar()有一个非常具体的用途。如果您接近日期,它将返回“今天上午9:00”之类的值。如果这不是您的预期,则应使用.format()函数。同样,您可能想要传递格式说明符。

  • To answer your questions in comments, No - you don't need to call .local() or .utc().

    要在评论中回答您的问题,否 - 您无需调用.local()或.utc()。

Putting it all together:

把它们放在一起:

var ts = moment("10/15/2014 9:00", "M/D/YYYY H:mm").valueOf();
var m = moment(ts);
var s = m.format("M/D/YYYY H:mm");
alert("Values are: ts = " + ts + ", s = " + s);

On my machine, in the US Pacific time zone, it results in:

在我的机器上,在美国太平洋时区,它导致:

Values are: ts = 1413388800000, s = 10/15/2014 9:00

值为:ts = 1413388800000,s = 10/15/2014 9:00

Since the input value is interpreted in terms of local time, you will get a different value for ts if you are in a different time zone.

由于输入值是根据本地时间解释的,因此如果您位于不同的时区,则会获得不同的ts值。

Also note that if you really do want to work with whole seconds (possibly losing precision), moment has methods for that as well. You would use .unix() to return the timestamp in whole seconds, and moment.unix(ts) to parse it back to a moment.

另请注意,如果您确实希望使用整秒(可能会丢失精度),那么也有方法。您可以使用.unix()以整秒返回时间戳,并使用moment.unix(ts)将其解析为片刻。

var ts = moment("10/15/2014 9:00", "M/D/YYYY H:mm").unix();
var m = moment.unix(ts);

#2


6  

http://momentjs.com/docs/#/displaying/unix-timestamp/

You get the number of unix seconds, not milliseconds!

你得到unix秒的数量,而不是毫秒!

You you need to multiply it with 1000 or using valueOf() and don't forget to use a formatter, since you are using a non ISO 8601 format. And if you forget to pass the formatter, the date will be parsed in the UTC timezone or as an invalid date.

您需要将它乘以1000或使用valueOf()并且不要忘记使用格式化程序,因为您使用的是非ISO 8601格式。如果您忘记传递格式化程序,则日期将在UTC时区中解析或作为无效日期进行解析。

moment("10/15/2014 9:00", "MM/DD/YYYY HH:mm").valueOf()