欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS中 new Date() 各方法的用法說明

 更新時間:2022年12月19日 16:10:35   作者:Xie_bro777  
這篇文章主要介紹了JS中 new Date() 各方法的用法,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

JS中 new Date() 各方法的用法

1.new Date() 參數(shù)篇

a.返回類型為國標(biāo)時間,
b.無參數(shù)時可以直接返回輸出時的時間,
c.有參數(shù)時則返回對應(yīng)時間的國標(biāo)時間,
d.日期中間的符號可以為,(英文逗號) - / . * = !@ # ¥ % & ,不可為~ · ` ^ + ,(中文逗號) 。

new Date()
Sun Aug 21 2022 15:22:09 GMT+0800 (中國標(biāo)準(zhǔn)時間)

new Date("2022-01-01")
Sat Jan 01 2022 08:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時間)

new Date("2022/01/01")
Sat Jan 01 2022 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時間)

new Date("2022.01.01")
Sat Jan 01 2022 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時間)

new Date("2022*01*01")
Sat Jan 01 2022 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時間)

new Date("Sat Jan 2022")
Sat Jan 01 2022 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時間) 

注1:英文括號 都為左括號 或一對括號 ,兩個右括號無法識別

new Date("2022(01)01")
Sat Jan 01 2022 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時間)

new Date("2022(01(01")
Sat Jan 01 2022 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時間)

new Date("2022)01)01")
Invalid Date 	// 無效的時間

注2:也可以用六個參數(shù)表示日期時間的各個數(shù)值
其中第二個參數(shù)代表月份減一,即參數(shù)為1時,其實是二月,第三個參數(shù)為0,代表上個月的最后一天

new Date("2022","01",0,11,12,20)
Mon Jan 31 2022 11:12:20 GMT+0800 (中國標(biāo)準(zhǔn)時間)

注3:入?yún)闀r間戳?xí)r 返回對應(yīng)的國標(biāo)時間

new Date(1661051533000)		
//Sun Aug 21 2022 11:12:13 GMT+0800 (中國標(biāo)準(zhǔn)時間)

2.方法篇

查詢一個月有多少天

//2022年一月份的天數(shù)
new Date("2022","01",0).getDate() 				// 31

日常方法

入?yún)⑷掌诘?時間戳
new Date("2022-08-21 11:12:13").getTime() 		// 1661051533000

入?yún)⑷掌诘?星期(注:日:0 ,一:1,二:2,三:3,四:4,五:5,六:6)
new Date("2022-08-21 11:12:13").getDay()        // 0

入?yún)⑷掌诘?年
new Date("2022-08-21 11:12:13").getFullYear()  	// 2022

入?yún)⑷掌诘?月 -1 
new Date("2022-08-21 11:12:13").getMonth()  	// 7

入?yún)⑷掌诘?日
new Date("2022-08-21 11:12:13").getDate() 		// 21

入?yún)⑷掌诘?時
new Date("2022-08-21 11:12:13").getHours() 		// 11

入?yún)⑷掌诘?分
new Date("2022-08-21 11:12:13").getMinutes()  	// 12

入?yún)⑷掌诘?秒
new Date("2022-08-21 11:12:13").getSeconds() 	// 13

入?yún)⑷掌诘?毫秒 (注:最大為999)
new Date("2022-08-21 11:12:13:999").getMilliseconds()  //999

入?yún)⑷掌?距 1900年的年數(shù)

new Date("2022-08-21 11:12:13").getYear() 		// 122

3.國標(biāo)時間、時間戳、年月日 時分秒的轉(zhuǎn)換

//vue  js 文件
handlerZero(param){
	param= param<10?('0'+param):param
},
// 國標(biāo)時間 轉(zhuǎn) 年月日 時分秒
formatDateTime(date) {
    let y = date.getFullYear()
    let m = date.getMonth()+1
    let d = date.getDate()
    let h = date.getHours()
    let h = date.getHours()
    let mi = date.getMinutes()
    let ss = date.getSeconds()
    return y+this.handlerZero(m)+this.handlerZero(d)+this.handlerZero(h)+this.handlerZero(mi)+this.handlerZero(ss)
},
// 時間戳轉(zhuǎn)年月日 時分秒
formatDateTime2(date) {
    let datee = new Date(date)
    return this.formatDateTime(datee)
},
// 年月日 轉(zhuǎn) 時間戳
formatDateTime3(date) {
    let y = date.substring(0,4)
    let m = date.substring(4,6)
    let d = date.substring(6,8)
    let str = y+'-'+m+'-'+d 
    return new Date(str).getTime()
}

到此這篇關(guān)于JS中 new Date() 各方法的用法的文章就介紹到這了,更多相關(guān)JS中 new Date() 各方法的用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論