简体   繁体   English

日期不正确地处理Unix日期(或者我使用的日期不正确?)

[英]Date Handling Unix Date Incorrectly (or I'm using Date incorrectly?)

I have the following data structure. 我有以下数据结构。 The first column is intervals. 第一列是间隔。 The first row of the interval datum is a unix time and the subsequent data are intervals (ie 300*1, 300*2, ect). 间隔数据的第一行是unix时间,后续数据是间隔(即300 * 1、300 * 2等)。 The other column is the data values. 另一列是数据值。 Here is the head of the data: 这是数据的标题:

a1521207300,555.45
1,554.53
2,554.07
3,553.9
4,552.67

And here I went about converting the unix time to a Date object. 在这里,我将Unix时间转换为Date对象。 The a here is ornamental, so I slice() at 1 like so: 这里的a是装饰性的,因此我像这样在1处slice()

    var rawTime = data[0].interval;
    var timeValue = Math.round(rawTime.slice(1));
    console.log(timeValue)
    console.log(new Date(timeValue))

I also tried using parseInt() instead of round() . 我也尝试使用parseInt()代替round() The console shows that this unix time is equivalent to: Jan 18 1970 which I had quite the guffaw at. 控制台显示此unix时间等效于: Jan 18 1970 ,我遭受了相当大的骚扰。 Then I got to thinking, maybe I did something wrong. 然后我开始思考,也许我做错了什么。 It's supposed to be a very recent date -- March 16th 2018. This is strange because my understanding is that javascript can be passed a unix date directly as per this answer . 它应该是最近的日期-2018年3月16日。这很奇怪,因为我的理解是,按照此答案 ,可以直接将javascript日期传递给unix日期。

I also checked the unix time at a conversion site: www.onlineconversion.com/unix_time.htm 我还在转换站点上检查了Unix时间:www.onlineconversion.com/unix_time.htm

Which confirmed that it's indeed a March 16th 2018 timestamp. 这证实了确实是2018年3月16日的时间戳。

Question: Why is this unix date for my March 2018 data being treated like a 1970's date? 问题:为什么我2018年3月数据的这个unix日期被视为1970年的日期? Maybe the a is actually doing something after all... Anyway, what is the correct way to handle this time stamp? 也许a毕竟实际上在做某事...无论如何,处理此时间戳的正确方法是什么? It's only 10 numerical digits, it does not seem to be a precision problem. 它只有10个数字,这似乎不是一个精度问题。 Date can handle unix times up to 13 digits I believe. 我相信Date最多可以处理unix时间的13位数字。

As per the documentation , when you invoke new Date(value) with an integer value, it is used as the number of milliseconds since January 1, 1970. To get the date you want, the value 1521207300 appears to be number of seconds instead of milliseconds. 根据文档 ,当您使用整数值调用new Date(value)时,它将用作自1970年1月1日以来的毫秒数。要获取所需的日期,值1521207300似乎是秒数,而不是毫秒。 That is, you missed a factor of 1000. new Date(1521207300000) gives Fri Mar 16 2018. 也就是说,您错过了1000倍new Date(1521207300000)给出了2018年3月16日。

When I take away new from new Date it seems to be ok. 当我从新日期取走新东西时,似乎还可以。 Not sure why though. 不知道为什么。

The documentation mentions the different behavior: 该文档提到了不同的行为:

Note: JavaScript Date objects can only be instantiated by calling JavaScript Date as a constructor: calling it as a regular function (ie without the new operator) will return a string rather than a Date object; 注意:JavaScript Date对象只能通过调用JavaScript Date作为构造函数来实例化:将其作为常规函数调用(即不使用new运算符)将返回字符串,而不是Date对象; unlike other JavaScript object types, JavaScript Date objects have no literal syntax. 与其他JavaScript对象类型不同,JavaScript Date对象没有文字语法。

It seems when called as a function Date(value) , it treats the value as the number of seconds, instead of milliseconds. 似乎当作为函数Date(value)调用时,它将值视为秒数,而不是毫秒。 I didn't dig deep enough to confirm this, because it doesn't matter: the documentation says to not use it this way (and since it gives a string instead of a date object, it's not very useful anyway). 我没有深入研究以确认这一点,因为这无关紧要:文档说不要以这种方式使用它(并且由于它提供的是字符串而不是日期对象,因此它并不是很有用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM