Check time difference in Javascript

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

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

18条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 04:42

    I like doing it via Epoch.

    var now = new Date();
    var future = new Date(now.setMinutes(15));
    
    var futureEpoch = moment(future).unix();
    var nowEpoch = moment(now).unix();
    var differenceInEpoch = nowEpoch - scheduledEpoch ;
    
    console.log("futureEpoch        : " + futureEpoch);
    console.log("nowEpoch              : " + nowEpoch);
    console.log("differenceInEpoch     : " + differenceInEpoch);
    
    var diffTime = new Date(0); // The 0 there is the key, which sets the date to the epoch
    diffTime.setUTCSeconds(differenceInEpoch);
    console.log("new diffTime : " + diffTime);
    

提交回复
热议问题