如何获得两个日期之间的差

如何获得两个日期之间的差

This question already has answers here:
Get difference between 2 dates in JavaScript? [duplicate]
                                
                                    (6个答案)
                                
                        
                                5年前关闭。
            
                    
如何获得两个日期之间的差额?

我有:

date1 = 2014/2/20
date2 = 2014/2/16


我想将差值作为4

最佳答案

Fiddle Demo

var oneDay = 24 * 60 * 60 * 1000;
function getDateDiffinDays(d1, d2) {
    var firstDate = new Date(d1);
    var secondDate = new Date(d2);
    return Math.round(Math.abs((firstDate.getTime() - secondDate.getTime()) / (oneDay)));
}

alert(getDateDiffinDays('2014/2/20', '2014/2/16'));

关于javascript - 如何获得两个日期之间的差异? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21809805/

10-08 22:57