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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26392280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:50:21  来源:igfitidea点击:

Using momentjs to convert date to epoch then back to date

javascriptdatemomentjs

提问by Ian Davis

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.

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

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

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

回答by Matt Johnson-Pint

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()returns Unix Time in whole seconds, but the default momentconstructor accepts a timestamp in milliseconds. You should instead use .valueOf()to return milliseconds. Note that calling .unix()*1000would also work, but it would result in a loss of precision.

  • 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 stringthat matches the expected input, such as:

    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.

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

  • 第一,术语。 “纪元”是指事物的起点。“Unix 时代”是 UTC 时间 1970 年 1 月 1 日午夜。您不能将任意的“日期字符串转换为纪元”。您可能指的是"Unix Time",它通常被错误地称为 "Epoch Time"。

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

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

    moment("10/15/2014 9:00", "M/D/YYYY H:mm")
    
  • .calendar()有一个非常特殊的用途。如果您接近日期,它将返回一个类似“今天上午 9:00”的值。如果这不是您所期望的,则应改用该.format()函数。同样,您可能希望传递格式说明符。

  • 要在评论中回答您的问题,不 - 您不需要致电.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 tsif 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.

还要注意,如果你真的想用整秒(可能会失去精度)工作,那么 moment 也有方法。您将使用.unix()以整秒为单位返回时间戳,并将moment.unix(ts)其解析为片刻。

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

回答by timaschew

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

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

You get the number of unix seconds, not milliseconds!

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

You you need to multiply it with 1000or 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()