简体   繁体   English

JavaScript Date.getTime函数未返回正确的值,或者我做错了

[英]Javascript Date.getTime function doesn't return the correct value or I'm doing something wrong

I'm trying to calculate timestamp for a given date (hour and minute is not important for now) . 我正在尝试计算给定日期的时间戳(小时和分钟目前不重要)。
I'm not looking for calculating days between two dates but as a sample of problem with computed timestamp lets take a look at the following snippet which calculate the days between two date : 我不是要计算两个日期之间的天数,而是作为计算时间戳的问题样本,让我们看一下下面的代码段,该代码段计算了两个日期之间的天数:

console.log(((new Date(2017,2,10)).getTime() - (new Date(2017,1,31)).getTime())/86400000);

this snippet print 7 for me while if I did the math correctly it actually should prints 10 . 此代码段对我来说是7,而如果我正确地进行数学运算,它实际上应该可以打印10。 I know there is something wrong with my code but I don't know what is it. 我知道我的代码有问题,但是我不知道这是什么。

Months in the Date constructor are 0-indexed, so 0 is January, 1 is February, etc. and moreover the parameters can overflow ( new Date(2017,11,32) is actually Mon Jan 01 2018 00:00:00 ). Date构造函数中的月份是0索引的,所以0是一月,1是二月, new Date(2017,11,32) ,而且参数可能会溢出( new Date(2017,11,32)实际上是Mon Jan 01 2018 00:00:00 )。 So in your example the dates are in fact: 因此,在您的示例中,日期实际上是:

new Date(2017,1,31)
Fri Mar 03 2017 00:00:00
new Date(2017,2,10)
Fri Mar 10 2017 00:00:00

Therefore the result is correct. 因此结果是正确的。

The value being returned in your code is absolutely fine. 在您的代码中返回的值绝对正确。 According to the Javascript MDN Documentation , for the Month parameter in the function, you can pass the following allowed values, otherwise they are automatically adjusted- 根据Javascript MDN Documentation ,对于函数中的Month参数,您可以传递以下允许值,否则将自动调整它们-

Integer value representing the month, beginning with 0 for January to 11 for December. 代表月份的整数值,从1月的0到12月的11开始。

So, in your case, since 31 February 2017 doesn't exist, the Date object is automatically adjusted and initialized with a value of 03 March 2017 . 因此,在您的情况下,由于不存在31 February 2017 ,因此Date对象将自动调整并初始化为03 March 2017 And the other date is initialised with a value of 10 March 2017 . 另一个日期初始值为10 March 2017 Thus, the difference is coming correctly as 7 days. 因此,差异将正确地显示为7天。 Here is a short note on the adjustment from MDN documentation- 这是MDN文档中有关调整的简短说明-

Where Date is called as a constructor with more than one argument, if values are greater than their logical range (eg 13 is provided as the month value or 70 for the minute value), the adjacent value will be adjusted. 如果将Date用作具有多个自变量的构造函数,则如果值大于其逻辑范围(例如,将13作为月份值或将70作为分钟值),则会调整相邻的值。 Eg new Date(2013, 13, 1) is equivalent to new Date(2014, 1, 1), both create a date for 2014-02-01 (note that the month is 0-based). 例如,新的Date(2013,13,1)等同于新的Date(2014,1,1),两者都为2014-02-01创建一个日期(请注意,该月是从0开始的)。 Similarly for other values: new Date(2013, 2, 1, 0, 70) is equivalent to new Date(2013, 2, 1, 1, 10) which both create a date for 2013-03-01T01:10:00. 同样,对于其他值:new Date(2013,2,1,0,70)等效于new Date(2013,2,1,1,10),两者都为2013-03-01T01:10:00创建一个日期。

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

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