timg.jpg
时间从未停止过,人生从未平淡过。时间每分每秒的走着,生命也是如此。
<input type="text" placeholder="请输入时间">
<button>开始</button>
<h2></h2>
<script>
var h = document.getElementsByTagName('h2')[0];
//倒计时方法
function countDown(stoptimestr) {
//获取当前时间
var nowTime = new Date();
//获取截止时间
var stopTime = new Date(stoptimestr);
console.log(stopTime.toLocaleString());
//获取毫秒数,并进行相减,求出时间差
var mistiming = stopTime.getTime() - nowTime.getTime();
//根据时间差 转换天数,时分秒
var days = Math.floor(mistiming / 1000 / 60 / 60 / 24);
var hours = Math.floor(mistiming / 1000 / 60 / 60 % 24);
var minutes = Math.floor(mistiming / 1000 / 60 % 60);
var seconds = Math.floor(mistiming / 1000 % 60);
days < 10 ? days = '0' + days : days;
hours < 10 ? hours = '0' + hours : hours;
minutes < 10 ? minutes = '0' + minutes : minutes;
seconds < 10 ? seconds = '0' + seconds : seconds;
var rels = mistiming > 0 ? `${days}天${hours}时${minutes}分${seconds}秒` : '请重新输入时间';
return rels;
}
// 启动倒计时
setInterval(function () {
h.innerHTML = countDown('2020-07-30 00:00:00');
}, 1000)
2020年7月毕业,离毕业还有还有一年啊!!!
image.png