您好,登錄后才能下訂單哦!
本文實例為大家分享了js Date對象應用3個實例,供大家參考,具體內容如下
一.獲取日期時間,秒數實時跳動
<!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)對分鐘和秒進行判斷,如果小于10,在其前補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();//小時(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執行一次,實現秒數實時跳動。 } </script> </body> </html>
二.高考倒計時功能(天)
<!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();//獲取當前時間 var timedate = new Date("2018,12,23");//截止時間 //getTime() 方法可返回距 1970 年 1 月 1 日之間的毫秒數。 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>
三.限時搶
<!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();//獲取當前時間 var timedate = new Date("2017/10/30,24:00:00");//截止時間 //getTime() 方法可返回距 1970 年 1 月 1 日之間的毫秒數。 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 + "小時" + m + "分鐘" + s + "秒"; document.getElementById('date').innerHTML = time; setTimeout(showTime,500); } </script> </body> </html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。