显示时间到页面上
<div id="app"></div>
<script>
setInterval(show,1000)
function show() {
var app = document.getElementById("app")
var d = new Date();
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
var sum = handleNum(hours) + ":" + handleNum(minutes) + ":" + handleNum(seconds);
console.log(sum)
app.innerHTML = sum;
}
function handleNum(num) {
if (num < 10) {
num = "0" + num;
}
return num;
}
</script>
距离下个时间 多少小时 多少分钟 多少秒
var time = "2021-10-13 17:10:10"
/* 多个小时,多分钟,多少秒 */;
/* 1、获取当前时间的毫秒数 */
var d = new Date();
// console.log(d.getTime())
var startTime = d.getTime();
/* 2、获取活动开始时间的毫秒 */
var endTime = new Date(time).getTime();
// console.log(endTime)
/* 3、得到中间间隔的毫秒数 */
var gapTime = endTime -startTime;
var seconds = Math.floor(gapTime/1000)
console.log(seconds)
var hours = Math.floor(seconds/3600);
console.log(hours)
seconds = seconds - 3600*hours;
var minutes = Math.floor(seconds/60);
console.log(minutes)
seconds = seconds - minutes*60;
console.log(seconds)
距离某天还有几天几小时几分
<script>
/* var time ="2021-10-19 08:30:00" 还有几天几小时几分 */
var time = "2021-10-19 08:30:00"
var startTime = (new Date()).getTime();
var endTime = (new Date(time)).getTime();
var gapTime = endTime-startTime;
var minutes = Math.floor(gapTime/(1000*60));
/*
8201分
1h = 60m
1t = 60*24
*/
// console.log(minutes)
var days = Math.floor(minutes/(60*24));
minutes = minutes -days*(60*24)
console.log(days)
var hours = Math.floor(minutes/60);
console.log(hours)
minutes = minutes - hours*60;
console.log(minutes)
</script>