var oDate = new Date();// console.log(oDate);// console.log(oDate.getFullYear());//年份// console.log(oDate.getMonth());//月份 0-11// console.log(oDate.getDate());//日期// console.log(oDate.getDay());//星期 0-6 0周日// console.log(oDate.getHours());//小时// console.log(oDate.getMinutes());//分钟// console.log(oDate.getSeconds());//秒// console.log(oDate.getTime());//1970年1月1日到现在的毫秒数 // oDate.setFullYear(2020);// console.log(oDate.getFullYear());
小例子,淘宝倒计时
时 分 秒
// 淘宝倒计时 var oSpan = document.getElementsByTagName("span"); var endDate = new Date("2016/2/17,12:00:00"); var endTime = endDate.getTime(); var time = setInterval(function(){ var nowDate = new Date(); var nowTime = nowDate.getTime(); var timeLeft = endTime - nowTime;// console.log(timeLeft); var nowHour = Math.floor(timeLeft/(1000*60*60)); var hourLeft = timeLeft%(1000*60*60); var nowMinute = Math.floor(hourLeft/(1000*60)); var minuteLeft = hourLeft%(1000*60); var nowSecond = Math.floor(minuteLeft/1000); oSpan[0].innerHTML = nowHour; oSpan[1].innerHTML = nowMinute; oSpan[2].innerHTML = nowSecond; if(timeLeft<=0){ clearInterval(time); oSpan[0].innerHTML = 0; oSpan[1].innerHTML = 0; oSpan[2].innerHTML = 0; } },500)