Calculate time difference between two times javascript

后端 未结 2 1795
攒了一身酷
攒了一身酷 2020-12-10 15:37

i\'ve looking around how to do this and i found a lot of examples with complicated code. Im using this:

var time1 = new Date();
var time1ms= time1.getTime(ti         


        
2条回答
  •  心在旅途
    2020-12-10 16:16

    Note that in the getTimezoneOffset() returns a value in minutes, so if you want to use the lapse, you can correct it for the timezone difference like this:

    lapse = new Date(difference); 
    tz_correction_minutes = new Date().getTimezoneOffset() - lapse.getTimezoneOffset();
    lapse.setMinutes(offset_date.getMinutes() + tz_correction_minutes);
    

    now you can do:

    label.text(lapse.getDate()-1+' days and'  +lapse.getHours()+':'+lapse.getMinutes()+':'+lapse.getSeconds());
    

    to print out the time difference in human readable form

提交回复
热议问题