简体   繁体   English

当两个月的日期不同时,如何获取两个日期之间的差异

[英]How to get difference between two dates in javascript when they are of different months

I want to get the difference between two dates when they are in different months or years. 我想了解两个日期在不同月份或年份时的区别。 I have used the code below 我用下面的代码

var d1= lastexecdate.split("."); var d1 = lastexecdate.split(“。”);

var d2= jabusinessdate.split("."); var d2 = jabusinessdate.split(“。”);

var date1= new Date(d1[2], d1[1],d1[0]); var date1 = new Date(d1 [2],d1 [1],d1 [0]);

var date2= new Date(d2[2], d2[1],d2[0]); var date2 = new Date(d2 [2],d2 [1],d2 [0]);

var timeDiff= (date2.getTime() - date1.getTime()); var timeDiff =(date2.getTime()-date1.getTime());

var diffdays= Math.ceil(timeDiff/(1000*3600*24)); var diffdays = Math.ceil(timeDiff /(1000 * 3600 * 24));

When i use dates such as 17.10.2014 and 19.10.2014 it gives me a differenc of two which is perfect. 当我使用2014年10月17日和2014年10月19日这样的日期时,它给了我两个完美的区别。 But when i use dates such as 31.10.2014 and 1.11.2014 it gives me a result as zero where as the actual result should have been 1. That is when the months or year are different it shows anomaly in result. 但是,当我使用2014年10月31日和2014年11月11日这样的日期时,它给我的结果为零,而实际结果应该是1。也就是说,当月份或年份不同时,它会显示异常结果。 Please suggest me some ideas or some functions which i can use to get appropriate result. 请给我建议一些想法或一些功能,我可以用它们来获得适当的结果。

In JavaScript months are indexed from 0. So January has index 0 and December has index 11. In your case when you are specifying the date 31.10.2014, you have to actually pass 9 as the month parameter to the date constructor. 在JavaScript中,月份从0开始索引。因此,一月份的索引为0,十二月的索引为11。在您指定2014年10月31日的情况下,您实际上必须将9作为month参数传递给日期构造函数。 Similarly 10 instead of 11 to the date constructor. 与日期构造器类似,而不是11。

Example: 例:

For "31.10.2014": 对于“ 31.10.2014”:

var date1 = new Date(2014,9,31);

For "1.11.2014": 对于“ 1.11.2014”:

var date2 = new Date(2014,10,1);

Now you do the calculation as follows: 现在,您可以按照以下步骤进行计算:

var timeDiff =Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 

this will give result 1. 这将得出结果1。

so all you have to do is subtract 1 from the month in your code as follows: 因此,您要做的就是从代码中的月份中减去1,如下所示:

var date1= new Date(d1[2], d1[1] - 1,d1[0]);

var date2= new Date(d2[2], d2[1] - 1,d2[0]);

Here is a function that will do that. 这是一个可以做到的功能。 I tested with different years as well as different months. 我测试了不同的年份以及不同的月份。 Since the time information is irrelevant for you, take it out of the way by giving it the same value for both dates, such as 00:00:00 as you stated in your comment 由于时间信息对您而言无关紧要,因此请为两个日期提供相同的值,例如您在评论中指出的00:00:00,以消除时间信息

function dateDiff(date1, date2){
    var diff = {}                           // return initialisation
    var tmp = date2 - date1;

    tmp = Math.floor(tmp/1000);             // Number of seconds between the     two dates
    diff.sec = tmp % 60;                    // Extracting the number of    seconds

    tmp = Math.floor((tmp-diff.sec)/60);    // Number of minutes (integer    part)
    diff.min = tmp % 60;                    // Extracting the number of     minutes

    tmp = Math.floor((tmp-diff.min)/60);    // Number of hours (integer     part)
    diff.hour = tmp % 24;                   // Extracting the number of     hours

    tmp = Math.floor((tmp-diff.hour)/24);   // Number of remaining days
    diff.day = tmp;

    return diff;
}

    date1 = new Date('2013-08-25 00:00:00');
    date2 = new Date('2013-10-29 22:16:57');
    diff = dateDiff(date1, date2);
    alert("Between "+date1.toString()+" and "+date2.toString()+" there are         "+diff.day+" days, "+diff.hour+" hours, "+diff.min+" minutes and "+diff.sec+"       seconds");

Here is two image captures of obtained result. 这是获得的结果的两个图像捕获。 First with different years and second with different months. 首先是不同的年份,其次是不同的月份。

年不同

几个月不同

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

相关问题 获取月份差异,并在JavaScript中两个日期之间的数组中列出月份 - Get difference in months and list the months in an array between two dates in javascript JavaScript 中两个日期之间的月差 - Difference in Months between two dates in JavaScript JavaScript:获取两个日期之间的所有月份? - JavaScript: get all months between two dates? 如何在JavaScript中的两个日期之间获取整月 - How to get full months between two dates in javascript 获取两个日期之间的月份 JavaScript - Get the months between two dates JavaScript 如何摆脱 JavaScript 中的天数错误。 在不同月份的两个连续日期获得相同的年龄差异 - How to get rid off age in days error in JavaScript. getting same age difference for two continue dates of different months JavaScript 中两个日期之间的年、月、日差 - Difference between two dates in years, months, days in JavaScript 我可以通过momentjs得出年份和月份中两个日期之间的差额吗? - Can I get the difference between two dates in years and months with momentjs? 如何计算JavaScript中两个日期之间的年,月和天的差异? - How can I calculate the difference in years, months and days between two dates in Javascript? 如何使用Moment.js获得年份和月份中两个日期之间的差异? - How to get difference between two dates in years and months using Moment.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM