本文實(shí)例為大家分享了js Date對象應(yīng)用3個(gè)實(shí)例,供大家參考,具體內(nèi)容如下
一.獲取日期時(shí)間,秒數(shù)實(shí)時(shí)跳動(dòng)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>date01</title> <style> #date{ position: absolute; font-size: 30px; font-family: '微軟雅黑'; top:50%; left:50%; -webkit-transform:translate(-50%,-50%); } </style> </head> <body> <div id="date">2017.10.29</div> <script> window.onload = function(){ showTime(); } //checkTime(i)對分鐘和秒進(jìn)行判斷,如果小于10,在其前補(bǔ)0. function checkTime(i){ return i < 10? "0"+i:i; } function showTime(){ var time = new Date();//日期對象 console.log("new Date :"+time); var year_1 = time.getYear();//獲得年份 console.log("getYear(): "+year_1); var year_2 = time.getFullYear();//獲得完整年份 console.log("getFullYear(): "+year_2); var month = time.getMonth();//獲得月份(0~11) console.log("getMonth(): "+month); var date = time.getDate();//獲得日期(1~31) console.log("getDate(): "+date); var day = time.getDay();//星期幾(0~6) console.log("getDay(): "+day) var hours = time.getHours();//小時(shí)(0~23) console.log("getHours(): "+hours); var min = time.getMinutes();//分鐘(0~59) console.log("getMinutes(): "+min); var seconds = time.getSeconds();//秒 console.log("getSeconds(): "+seconds); min = checkTime(min); seconds = checkTime(seconds); var weekday = new Array(7); weekday[0] = "星期天"; weekday[1] = "星期一"; weekday[2] = "星期二"; weekday[3] = "星期三"; weekday[4] = "星期四"; weekday[5] = "星期五"; weekday[6] = "星期六"; var str_time = year_2 + "年" + (month+1) + "月" + date + "日" +" "+ weekday[day] +" "+ hours +":" + min +":" + seconds; document.getElementById('date').innerHTML = str_time; setTimeout(showTime,500);//每500ms執(zhí)行一次,實(shí)現(xiàn)秒數(shù)實(shí)時(shí)跳動(dòng)。 } </script> </body> </html>
二.高考倒計(jì)時(shí)功能(天)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>date02</title> <style> #date{ position: absolute; font-size: 30px; font-family: '微軟雅黑'; top:50%; left:50%; -webkit-transform:translate(-50%,-50%); } </style> </head> <body> <div id="date">2017.10.29</div> <script> window.onload = function(){ var now = new Date();//獲取當(dāng)前時(shí)間 var timedate = new Date("2018,12,23");//截止時(shí)間 //getTime() 方法可返回距 1970 年 1 月 1 日之間的毫秒數(shù)。 var date = timedate.getTime() - now.getTime(); //Math.ceil()向上取整 var time = Math.ceil(date/(24*60*60*1000)); document.getElementById('date').innerHTML = time; } </script> </body> </html>
三.限時(shí)搶
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>date03</title> <style> #date{ position: absolute; font-size: 30px; font-family: '微軟雅黑'; top:50%; left:50%; -webkit-transform:translate(-50%,-50%); } </style> </head> <body> <div id="date">2017.10.29</div> <script> window.onload = function(){ showTime(); } function checkTime(i){ return i < 10? "0"+i:i; } function showTime(){ var now = new Date();//獲取當(dāng)前時(shí)間 var timedate = new Date("2017/10/30,24:00:00");//截止時(shí)間 //getTime() 方法可返回距 1970 年 1 月 1 日之間的毫秒數(shù)。 var leftTime = (timedate.getTime() - now.getTime())/1000; var d = checkTime(parseInt(leftTime/(24*60*60))); var h = checkTime(parseInt(leftTime/(60*60)%24)); var m = checkTime(parseInt(leftTime/60%60)); var s = checkTime(parseInt(leftTime%60)); var time = d + "天" + h + "小時(shí)" + m + "分鐘" + s + "秒"; document.getElementById('date').innerHTML = time; setTimeout(showTime,500); } </script> </body> </html>
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com