日期对象
一、创建 Date 对象? 1.new Date()? ?当前日期和时间 ? 2.?new Date(milliseconds)? ? ??参数(milliseconds):从1970年1月1日00:00:00 UTC开始计算的毫秒数。如果将Unix时间戳作为参数,必须将Unix时间戳乘以1000。 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?【unix时间戳是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒】 ?3.??new Date(dateString)? ? ? ? ? ? 参数(dateString):一个日期字符串,返回所对应的时间。所有可以被Date.parse()方法解析的日期字符串,都可以当作Date对象的参数 ?4.??new Date(year,month,day,hours,minutes,seconds,milliseconds)? ??参数(year,milliseconds):对应的年、月、日、小时、分钟、秒和毫秒。 ? 二、关于日期的常用方法? ? get? ? ? ? ??getTime(): 返回实例对象当前时间距离1970年1月1日00:00:00对应的毫秒数 ? ?? ?set?? ? ? ? ? ?? setTime(): 以毫秒设置 Date 对象。 ? 转换为字符串? ? ? ? ? ? toString(): 把 Date 对象转换为字符串,并返回结果。 toUTCString(): 返回对应的UTC时间,也就是比北京时间晚8个小时。 toDateString(): 返回日期的字符串形式 toTimeString(): 返回时间的字符串形式。 ? ? Date.now():返回当前距离1970年1月1日00:00:00的毫秒数? parse():解析日期字符串,返回距离1970年1月1日 00:00:00的毫秒数? ? ? ? ? ? ? ? ? tip:日期字符串的格式应该完全或者部分符合YYYY-MM-DDTHH:mm:ss.sssZ格式,Z表示时区,选填 ? ? ? ? ? ? ? ? ? ? ??若解析失败,返回NaN ? ? ? ?? ?倒计时
function dowmTime() {
var endtime = new Date(‘2019-7-15 11:01‘);
var nowTime = new Date();
var differSecond = parseInt((endtime - nowTime) / 1000)
if (differSecond <= 0) {
clearInterval(flag)
document.getElementsByClassName(‘daybox‘)[0].children[0].innerText = 0;
document.getElementsByClassName(‘hourbox‘)[0].children[0].innerText = 0;
document.getElementsByClassName(‘minutebox‘)[0].children[0].innerText = 0;
document.getElementsByClassName(‘secondbox‘)[0].children[0].innerText = 0;
} else {
var day = Math.floor(differSecond / (24 * 60 * 60))
var hour = Math.floor(differSecond / (60 * 60)) - (day * 24);
var minute = Math.floor(differSecond / 60) - (day * 24 * 60) - (hour * 60);
var second = Math.floor(differSecond) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
if (hour <= 9) {
hour = ‘0‘ + hour;
}
if (minute <= 9) {
minute = ‘0‘ + minute;
}
if (second <= 9) {
second = ‘0‘ + second;
}
document.getElementsByClassName(‘daybox‘)[0].children[0].innerText = day;
document.getElementsByClassName(‘hourbox‘)[0].children[0].innerText = hour;
document.getElementsByClassName(‘minutebox‘)[0].children[0].innerText = minute;
document.getElementsByClassName(‘secondbox‘)[0].children[0].innerText = second;
}
}
let flag = setInterval(function () {
dowmTime()
},1000)
? 原文:https://blog.csdn.net/ai_u20/article/details/84845482 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |