免費(fèi)做h5的網(wǎng)站展示型網(wǎng)站有哪些
時(shí)間對(duì)象是一種復(fù)雜數(shù)據(jù)類(lèi)型,用來(lái)存儲(chǔ)時(shí)間
創(chuàng)建時(shí)間對(duì)象
內(nèi)置構(gòu)造函數(shù)創(chuàng)建
????????語(yǔ)法:var 時(shí)間名=new Date()
var date=new Date()console.log(date) //Wed May 29 2024 16:03:47 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)
創(chuàng)建指定日期
? ? ? ? 當(dāng)參數(shù)為數(shù)字——>在格林威治的時(shí)間基礎(chǔ)上增加
? ? ? ? ? ? ? ? 多個(gè)數(shù)字一次表示年月日時(shí)分秒
? ? ? ? 當(dāng)參數(shù)為字符串——>設(shè)置指定日期
//格林威治時(shí)間 Thu Jan 01 1970 08:00:00 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)//1.一個(gè)參數(shù) 在格林威治時(shí)間的基礎(chǔ)上加 1000=1svar date1=new Date(1000)console.log(date1) //Thu Jan 01 1970 08:00:01 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)//2.多個(gè)參數(shù) 依次表示年月日時(shí)分秒 在格林威治時(shí)間的基礎(chǔ)上加var date2=new Date(1,2,3)console.log(date2) //Sun Mar 03 1901 00:00:00 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)//3.當(dāng)參數(shù)為字符串,則是設(shè)置具體日期var date3=new Date('2001-12-12 10:10:10')console.log(date3) //Wed Dec 12 2001 10:10:10 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)var date4=new Date('2001/12/12 10:10:10')console.log(date4) //Wed Dec 12 2001 10:10:10 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)var date5=new Date('2001/12/12 10:10:10','2002/12/12 10:10:10')console.log(date5) //Invalid Date 無(wú)效日期
事件對(duì)象方法
var date=new Date('2024/12/2 10:10:10')//獲取年份console.log(date.getFullYear()) //2024//獲取月份 月是從0開(kāi)始到11結(jié)束 0-11——>1-12console.log(date.getMonth()) //返回?cái)?shù)字11 // 獲取日期console.log(date.getDate()) //2// 獲取時(shí)間console.log(date.getHours()) //10// 獲取分鐘console.log(date.getMinutes()) //10// 獲取秒console.log(date.getSeconds()) //10// 獲取毫秒console.log(date.getMilliseconds()) //0// 獲取星期幾 返回?cái)?shù)字0-6分別對(duì)應(yīng)星期日-星期六console.log(date.getDay()) //1// 獲取時(shí)間戳——現(xiàn)在距離格林威治時(shí)間的毫秒數(shù)console.log(date.getTime()) //1733105410000
練習(xí)題
練習(xí)題1:獲取當(dāng)前時(shí)間編寫(xiě)一個(gè)函數(shù),返回當(dāng)前的日期和時(shí)間字符串,格式為:YYYY-MM-DD-MM-DD HH:mm:ss。
function getCurrentDateTime() {var now = new Date();var year = now.getFullYear();var month = ("0" + (now.getMonth() + 1)).slice(-2);//slice從倒數(shù)第2位開(kāi)始截取var day = ("0" + now.getDate()).slice(-2);var hours = ("0" + now.getHours()).slice(-2);var minutes = ("0" + now.getMinutes()).slice(-2);var seconds = ("0" + now.getSeconds()).slice(-2);return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds; }console.log(getCurrentDateTime());
練習(xí)題2:編寫(xiě)一個(gè)函數(shù),輸入任意年月日,輸出該年份的日期是星期幾(例如:0代表周日,1代表周一,以此類(lèi)推)。
function fn(argDay) {var day = new Date(argDay);return day.getDay();}console.log(fn("2020/12/2")); //3console.log(fn("2023/01/12")); //4console.log(fn("2024/5/27")); //1
練習(xí)題3:倒計(jì)時(shí)創(chuàng)建一個(gè)倒計(jì)時(shí)器函數(shù),接受一個(gè)未來(lái)的時(shí)間(年-月-日 時(shí):分:秒),并實(shí)時(shí)顯示距離該時(shí)間還有多久(以小時(shí)、分鐘、秒顯示)。
function fn(d1) {var day1 = new Date();console.log(day1);var day2 = new Date(d1);//兩者相差毫秒數(shù)var timer = Math.abs(day1.getTime() - day2.getTime()) / 1000;console.log(timer);//1小時(shí)=60分鐘=3600秒 =3600 000毫秒var h = parseInt(timer / 3600);var m = parseInt((timer - h * 3600) / 60);var s = parseInt(timer - h * 3600 - m * 60);return h + ":" + m + ":" + s;}console.log(fn("2024/5/31 20:25:20"));
練習(xí)題4:日期比較編寫(xiě)一個(gè)函數(shù),比較兩個(gè)日期字符串(格式Y(jié)YYY-MM-DD),返回哪一個(gè)日期更早。
function compareDates(dateStr1, dateStr2) {var date1 = new Date(dateStr1);var date2 = new Date(dateStr2);return date1.getTime() < date2.getTime() ? dateStr1 : dateStr2; }console.log(compareDates("2023-01-01", "2023-12-31"));
練習(xí)題5:月份天數(shù)編寫(xiě)一個(gè)函數(shù),給定一個(gè)年份和月份,返回該月份的天數(shù)(考慮閏年)。
var date = new Date(2024, 2, 0); //將日期設(shè)置為0即可console.log(date.getDate()); //29