简体   繁体   English

如何使用JavaScript查找两个日期之间的天差

[英]How to find Days difference between two dates using javascript

I need to find days difference between two dates using javascript, here is my code 我需要使用JavaScript查找两个日期之间的天差,这是我的代码

I have start date and end date 我有开始日期和结束日期

var diff = Math.floor((Date.parse(enddate) - Date.parse(startdate)) / 86400000);

Its calculating the difference from current time. 它计算与当前时间的差。 I need to find the number of dates between given dates. 我需要找到给定日期之间的日期数。

For example if i give input start date as 17-dec-2014 and 19-dec-2014 its displaying two days, but i need to calculate number of days 17,18 and 19. It should display number of days as three. 例如,如果我输入的开始日期为2014年12月17日和2014年12月19日,则显示两天,但是我需要计算天数17,18和19。它应显示为三天。

Any one help me please? 有人可以帮我吗?

You can set the hours, minutes, seconds, and milliseconds to 0 before doing the comparison in order to ignore the time of day, eg: 您可以在进行比较之前将小时,分钟,秒和毫秒设置为0,以忽略一天中的时间,例如:

 var startdate = "2014-12-17"; var enddate = "2014-12-19"; var start = new Date(startdate); start.setHours(0, 0, 0, 0); // Sets hours, minutes, seconds, and milliseconds var end = new Date(enddate); end.setHours(0, 0, 0, 0); var diff = Math.round((end - start) / 86400000) + 1; // See note below re `+ 1` snippet.log("diff = " + diff); // 3 
 <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

Two notes on that: 关于这两个注意事项:

  1. Math.round : This is here because if the timespan crosses a Daylight Savings Time boundary, the number will be off by a small fraction, but within the realm where rounding corrects it. Math.round :这是在这里,因为如果时间跨度超过了夏令时边界,则该数字将减少一小部分,但会在舍入校正范围内。 Note that you must round , not truncate, floor, ceiling. 请注意,您必须舍入 ,截断地板,天花板,而不要截断。

  2. + 1 : The + 1 at the end of the diff = line is because your "difference" is unusual, because you're counting the start and end days inclusive. + 1diff =行末尾的+ 1是因为您的“差异”是不寻常的,因为您要计算开始和结束的天数。 That's very odd, it would say that the difference in days from one Monday to the next was eight , not seven, because it would count the Monday on either end. 这很奇怪,它会说从一个星期一到下一个星期一的天数差是8个 ,而不是7天,因为它将算在任一端的星期一。 But you said: 但是你说:

    For example if i give input start date as 17-dec-2014 and 19-dec-2014 its displaying two days, but i need to calculate number of days 17,18 and 19. 例如,如果我输入的开始日期为2014年12月17日和2014年12月19日,则显示的是两天,但我需要计算出天数17,18和19。

    ...so you need the + 1 . ...所以您需要+ 1 A normal difference between two dates wouldn't have it. 两个日期之间没有正常的区别。

Example across DST boundaries (in many timezones): 跨DST边界的示例(在许多时区中):

 var start, end, diff; start = new Date(2014, 2, 1); // March 1st 2014 end = new Date(2014, 5, 1); // May 1st 2014 diff = ((end - start) / (1000 * 3600 * 24)) + 1; // diff won't *quite* be 93, because of the change to DST // (assuming a timezone where DST changes sometime in // March, as in most parts of the US, UK, and Canada snippet.log("diff = " + diff + " instead of 93"); snippet.log("rounded = " + Math.round(diff)); // Similarly, at the other end: start = new Date(2014, 9, 1); // October 1st 2014 end = new Date(2014, 11, 1); // December 1st 2014 diff = ((end - start) / (1000 * 3600 * 24)) + 1; // diff won't *quite* be 62, because of the change to DST // (assuming a timezone where DST changes sometime in // March, as in most parts of the US, UK, and Canada snippet.log("diff = " + diff + " instead of 62"); snippet.log("rounded = " + Math.round(diff)); 
 <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

This is the kind of thing that makes me turn to a library like MomentJS . 这种事情使我转向像MomentJS这样的库。 Using MomentJS, it would be: 使用MomentJS,它将是:

var diff = moment(enddate).diff(moment(startdate), 'days') + 1;

...where again the + 1 is because of your unusual definition of the difference between two dates. ... + 1再次是因为您对两个日期之间的时差的不寻常定义。

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

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