Check time difference in Javascript

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

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

18条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 04:39

    You can Get Two Time Different with this function. 
    

     /**
         * Get Two Time Different
         * @param join
         * @param lastSeen
         * @param now
         * @returns {string}
         */
        function getTimeDiff( join, lastSeen, now = false)
        {
            let t1 = new Date(join).getTime(), t2 = new Date(lastSeen).getTime(), milliseconds =0, time ='';
            if (now) t2 = Date.now();
            if( isNaN(t1) || isNaN(t2) ) return '';
            if (t1 < t2) milliseconds = t2 - t1; else milliseconds = t1 - t2;
            var days = Math.floor(milliseconds / 1000 / 60 / (60 * 24));
            var date_diff = new Date( milliseconds );
            if (days > 0) time += days + 'd ';
            if (date_diff.getHours() > 0) time += date_diff.getHours() + 'h ';
            if (date_diff.getMinutes() > 0) time += date_diff.getMinutes() + 'm ';
            if (date_diff.getSeconds() > 0) time += date_diff.getSeconds() + 's ';
            return time;
        }
        
        
        console.log(getTimeDiff(1578852606608, 1579530945513));
        

提交回复
热议问题