自己写的时间
如:var startTime= ‘2019/12/06 19:00:00’;
只有这样 new Date(startTime).getTime()
才能把自己写的时间转化成毫秒数来进行加减乘除
获取当前时间
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String hh=simpleDateFormat.format(new Date());
毫秒数 利用Math.floor方法进行获取部分的时间
d=Math.floor(lefttime/1000/60/60/24); //天
h=Math.floor(lefttime/1000/60/60%24); //时
m=Math.floor(lefttime/1000/60%60); //分
s=Math.floor(lefttime/1000%60); //秒
新建的date可以直接用get获得部分的时间
var date=new Date();//获取当前时间(包括日期)
var year=date.getFullYear();
var month=date.getMonth()+1;
var day=date.getDate();
var hh=date.getHours();
var mm=date.getMinutes();
var ss=date.getSeconds();
一段时间运行一下这个方法
setTimeout(“disptime()”,1000)
<script type="text/javascript">
window.onload=function(){
a3();
}
//在number小于10的数组前补0
function checkTime(i){
if(i<10){
i="0"+i;
}
return i;
}
//3
function a3(){
var date=new Date();//获取当前时间(包括日期)
var startTime= '2019/12/06 19:00:00'; //开始时间
var testTime="7500000";//两小时零五分 2*60*60+5*60
var lefttime = testTime-(date-new Date(startTime).getTime()); //考试时间-(当前时间-开始时间)=倒计时
var d,h,m,s;
if(lefttime>0){
// d=Math.floor(lefttime/1000/60/60/24); //天
h=checkTime(Math.floor(lefttime/1000/60/60%24)); //时
m=checkTime(Math.floor(lefttime/1000/60%60)); //分
s=checkTime(Math.floor(lefttime/1000%60)); //秒
// lefttime=lefttime-1000; //每次进来将总时间减去1秒
document.getElementById("sy").innerHTML=" "+h+":"+m+":"+s;
var myTimer=setTimeout("a3()",1000);
}else{
document.getElementById("sy").innerHTML="跳转 ";
}
}
</script>
<body>
<div style="margin-top:8px;">
当前时间-开始时间: <font id="sy" color="red" ></font>
</div>
</body>
参考于:https://www.cnblogs.com/zhaosq/p/10918757.html
https://blog.csdn.net/firstday2014/article/details/87876322