使用momentjs将日期转换为纪元,然后再回溯 [英] Using momentjs to convert date to epoch then back to date

查看:157
本文介绍了使用momentjs将日期转换为纪元,然后再回溯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  var epoch = moment(10/15/2014 9:00)。unix(); //我需要做.local()吗? 
var momentDate = moment(epoch); //我也试过了moment.utc(epoch)
var momentDateStr = momentDate.calendar();
alert(values are:epoch =+ epoch +,momentDateStr =+ momentDateStr);

渲染

 值为:epoch = 1413378000,momentDateStr = 01/17/1970 

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

解决方案

这里有一些错误:




  • 时代是指某些事物的起点。 Unix Epoch是1970年1月1日UTC的午夜。您不能将任意的日期字符串转换为纪元。您可能意味着Unix时间,这通常被错误地称为时代时代。


  • .unix() 在整秒钟内返回Unix时间,但默认的时刻构造函数在 milliseconds 。您应该使用 .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()




把它们放在一起:

  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);

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


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


由于输入值是按照当地时间进行解释的,所以如果您处于不同的时区,您将获得 ts 的不同值。 p>

另请注意,如果您真的想要使用一整秒(可能会失去精度),那么时刻也有方法。您将使用 .unix()在整秒钟内返回时间戳,并将 moment.unix(ts)解析

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


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

解决方案

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 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.

  • 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:

    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().

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

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.

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.

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

这篇关于使用momentjs将日期转换为纪元,然后再回溯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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