获取时 分 秒 的封装
var date=new Date();
var h=date.getHours();
var f=date.getMinutes();
var m=date.getSeconds();
var sum=hNum(h)+":"+hNum(f)+":"+hNum(m);
console.log(sum);
function hNum(num){
if(num<10){
num="0"+num;
}
return num;
}
倒计时
<div id="app"></div>
<script>
setInterval(go,1000);
function go(){
var app =document.querySelector("#app")
var date=new Date();
var h=date.getHours();
var f=date.getMinutes();
var m=date.getSeconds();
var sum=hNum(h)+":"+hNum(f)+":"+hNum(m);
app.innerHTML=sum
console.log(sum);
}
function hNum(num){
if(num<10){
num="0"+num;
}
return num;
}
解析字符串获取距离时间戳
var s="2021:10:13,18:00:00";
// var arr=s.replace(","," ");
// console.log(arr.slice(","));
s=s.split(",");
var arr=[];
var reg=/\:/g;
arr[0]= s[0].replace(reg,"-");
arr[1]=s[1];
var list=arr.toString()
list=list.replace(","," ");
var startTime=(new Date()).getTime();
var endTime=(new Date(list)).getTime();
var gapTime=endTime-startTime;
//获取多少秒
var seconds=gapTime/1000;
//获取小时
var h=Math.floor(seconds/60/60);
//获取相差秒数
var mm=seconds-h*3600;
//获取分钟数
var f=Math.floor(mm/60);
//获取秒数
var m=Math.floor(mm-f*60);
console.log(m);
console.log(h);
console.log(f);
console.log("距离上晚自习还剩下"+h+"小时"+f+"分钟"+m+"秒");