<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body></body> </html> <script> function countTime(st) { const date = new Date(st); // 指定时间 date.setDate(date.getDate() + 1); // 将日期增加一天 var date1 = new Date(); //开始时间 var date2 = date; //结束时间 var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数 //计算出相差天数 var days = Math.floor(date3 / (24 * 3600 * 1000)); //计算时差 var hours = Math.floor(date3 % (24 * 3600 * 1000) / (3600 * 1000)); //计算分差 var minutes = Math.floor(date3 % (24 * 3600 * 1000) % (3600 * 1000) / (60 * 1000)); //计算秒差 var seconds = Math.round( date3 % (24 * 3600 * 1000) % (3600 * 1000) % (60 * 1000) / 1000); return `${days}天${hours}时${minutes}分${seconds}秒`; } document.write(countTime("2023-10-08 16:05:06")); </script>
标签:24,10,date3,3600,08,js,var,Math,1000 From: https://www.cnblogs.com/iuniko/p/17749858.html