Check time difference in Javascript

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

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

18条回答
  •  别那么骄傲
    2020-11-22 04:40

    Here is my rendition....

    function get_time_difference(earlierDate, laterDate) 
    {
        var oDiff = new Object();
    
        //  Calculate Differences
        //  -------------------------------------------------------------------  //
        var nTotalDiff = laterDate.getTime() - earlierDate.getTime();
    
        oDiff.days = Math.floor(nTotalDiff / 1000 / 60 / 60 / 24);
        nTotalDiff -= oDiff.days * 1000 * 60 * 60 * 24;
    
        oDiff.hours = Math.floor(nTotalDiff / 1000 / 60 / 60);
        nTotalDiff -= oDiff.hours * 1000 * 60 * 60;
    
        oDiff.minutes = Math.floor(nTotalDiff / 1000 / 60);
        nTotalDiff -= oDiff.minutes * 1000 * 60;
    
        oDiff.seconds = Math.floor(nTotalDiff / 1000);
        //  -------------------------------------------------------------------  //
    
        //  Format Duration
        //  -------------------------------------------------------------------  //
        //  Format Hours
        var hourtext = '00';
        if (oDiff.days > 0){ hourtext = String(oDiff.days);}
        if (hourtext.length == 1){hourtext = '0' + hourtext};
    
        //  Format Minutes
        var mintext = '00';
        if (oDiff.minutes > 0){ mintext = String(oDiff.minutes);}
        if (mintext.length == 1) { mintext = '0' + mintext };
    
        //  Format Seconds
        var sectext = '00';
        if (oDiff.seconds > 0) { sectext = String(oDiff.seconds); }
        if (sectext.length == 1) { sectext = '0' + sectext };
    
        //  Set Duration
        var sDuration = hourtext + ':' + mintext + ':' + sectext;
        oDiff.duration = sDuration;
        //  -------------------------------------------------------------------  //
    
        return oDiff;
    }
    

提交回复
热议问题