js计算2个时间的时间差(毫秒、天、时、分、秒)

2024年07月28日 建站教程

var startDate = '2022-02-15 11:30:00'
var endDate = '2022-02-15 09:00:00'

var newDate = new Date(startDate); 
var oldDate = new Date(endDate);

var diffDate = newDate - oldDate; //计算时间差毫秒

var days = parseInt(diffDate / (1000 * 60 * 60 * 24));
var hours = parseInt((diffDate % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = parseInt((diffDate % (1000 * 60 * 60)) / (1000 * 60));
var seconds = (diffDate % (1000 * 60)) / 1000;
console.log("毫秒: "+ diffDate);
console.log("天: "+ days);
console.log("时: "+ parseInt(days * 24 + hours));
console.log("分: "+ parseInt(days * 24 + hours * 60 + minutes));
console.log("秒:"+ parseInt(days * 24 + hours * 60 + minutes * 60 + seconds))

本文链接:http://so.lmcjl.com/news/9368/

展开阅读全文
相关内容