Difference Between Datetimes

后端 未结 2 1298
温柔的废话
温柔的废话 2021-01-28 17:56

I\'ve got a bit of code here that calculates the hours difference between two datetimes. Kind of at a loss here. Code is hit or miss and I\'m not sure why.

var          


        
相关标签:
2条回答
  • 2021-01-28 18:23

    date.js solves your problem:

    var first = Date.parse(01/09/2011 10:30 am);
    var second = Date.parse(01/11/2011 11:00 am);
    var diffMs = Math.abs(first - second) // difference in milliseconds
    
    0 讨论(0)
  • 2021-01-28 18:31

    The problem is that your parseInt() on date[1] is using the leading zero.

    return new Date(
        parseInt(date[2]), parseInt(date[0])-1, parseInt(date[1]),
        hm[0], parseInt(hm[1])
    );
    

    When you pass a date like 01/09/2011, the 09 is being parsed as 0, not 9.

    0 讨论(0)
提交回复
热议问题