Check time difference in Javascript

后端 未结 18 1143
梦毁少年i
梦毁少年i 2020-11-22 04:27

How would you check time difference from two text-boxes in Javascript?

18条回答
  •  粉色の甜心
    2020-11-22 04:43

    When i tried the difference between same time stamp it gave 0 Days 5 Hours 30 Minutes

    so to get it exactly i have subtracted 5 hours and 30 min

    function get_time_diff( datetime )
    {
    var datetime = typeof datetime !== 'undefined' ? datetime : "2014-01-01 01:02:03.123456";
    
    var datetime = new Date(datetime).getTime();
    var now = new Date().getTime();
    
    if( isNaN(datetime) )
    {
        return "";
    }
    
    console.log( datetime + " " + now);
    
    if (datetime < now) {
        var milisec_diff = now - datetime;
    }else{
        var milisec_diff = datetime - now;
    }
    
    var days = Math.floor(milisec_diff / 1000 / 60 / (60 * 24));
    
    var date_diff = new Date( milisec_diff );
    
    return days + "d "+ (date_diff.getHours() - 5) + "h " + (date_diff.getMinutes() - 30) + "m";
    }
    

提交回复
热议问题