Check time difference in Javascript

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

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

18条回答
  •  -上瘾入骨i
    2020-11-22 04:58

    Here's the solution that worked for me:

    var date1 = new Date("08/05/2015 23:41:20");
    var date2 = new Date("08/06/2015 02:56:32");
    
    var diff = date2.getTime() - date1.getTime();
    
    var msec = diff;
    var hh = Math.floor(msec / 1000 / 60 / 60);
    msec -= hh * 1000 * 60 * 60;
    var mm = Math.floor(msec / 1000 / 60);
    msec -= mm * 1000 * 60;
    var ss = Math.floor(msec / 1000);
    msec -= ss * 1000;
    
    alert(hh + ":" + mm + ":" + ss);
    

提交回复
热议问题