齊魯人才網(wǎng)泰安最新招聘網(wǎng)百度seo學(xué)院
Javascript Date 對象相關(guān)知識
參考文章@虹貓1992
創(chuàng)建 Date 對象.
方法一:
自動使用當(dāng)前的日期和時間作為其初始值.
var date = new Date();
方法二:將給定的毫秒數(shù)轉(zhuǎn)換為使用的時間,new Date(dateVal)
- 如果是數(shù)字值,
dateVal
表示指定日期與1970年1月1日午夜間全球標(biāo)準時間的毫秒數(shù)。- 如果是字符串,則
dateVal
按照parse
方法中的規(guī)則進行解析。
var date = new Date("2018/04/06 03:23:55");
var date = new Date(1545548361287);
方法三:
指定具體的日期,
new Date((year,month,date[,hours[,minutes[,seconds[,ms]]]]))
year
必選項,完整的年份,比如,1976
(而不是76
)
month
必選項,表示的月份,是從0
到11
之間的整數(shù)(1
月至12
月)
date
必選項,表示日期,是從1
到31
之間的整數(shù)
hours
可選項,如果提供了minutes
則必須給出。表示小時,是從0
到23
的整數(shù)
minutes
可選項,如果提供了seconds
則必須給出。表示分鐘,是從0
到59
的整數(shù)
seconds
可選項,如果提供了milliseconds
則必須給出。表示秒鐘,是從0
到59
的整數(shù)
ms
可選項,表示毫秒,是從0
到999
的整數(shù)
var date = new Date(2018, 11, 23, 15, 3, 23);
日期運算
兩個日期示例對象進行運算
- 加法:返回的是兩個字符串連接而成的新字符串。
- 減法:返回的是它們間隔的毫秒數(shù);
var d1 = new Date(2018, 0, 1);
var d2 = new Date(2018, 11, 1);console.log(d2 - d1); //28857600000
console.log(d2 + d1);
// "Sat Dec 01 2018 00:00:00 GMT+0800 (中國標(biāo)準時間)Mon Jan 01 2018 00:00:00 GMT+0800 (中國標(biāo)準時間)"
靜態(tài)方法
Date.now()
: 返回當(dāng)前時間距離時間零點(1970
年1
月1
日00:00:00 UTC
)的毫秒數(shù),相當(dāng)于Unix
時間戳乘以1000
。
Date.now(); // 1545549988630
繼承方法
valueOf()
: 返回實例對象距離時間零點(1970
年1
月1
日00:00:00 UTC
)對應(yīng)的毫秒數(shù),該方法等同于getTime
方法。
var d = new Date();d.valueOf() // 1545550458280
d.getTime() // 1545550458280
toString()
方法返回一個完整的日期字符串。
var d = new Date(2018, 11, 23);console.log(d.toString()); // Sun Dec 23 2018 00:00:00 GMT+0800 (中國標(biāo)準時間)
console.log(d); // Sun Dec 23 2018 00:00:00 GMT+0800 (中國標(biāo)準時間)
toLocaleString()
返回一個表示該日期對象的字符串,該字符串與系統(tǒng)設(shè)置的地區(qū)關(guān)聯(lián)。
var d = new Date(2018, 11, 23);console.log(d.toLocaleString()); // 2018/12/23 上午12:00:00
格式化方法
參考文章@lsxj
let d = new Date();
console.log(d.toLocaleString()); // 2023/9/19 上午11:26:21
console.log(d.toString()); // Tue Sep 19 2023 11:26:53 GMT+0800 (中國標(biāo)準時間)
console.log(d.toDateString()); // Tue Sep 19 2023
console.log(d.toTimeString()); // 11:27:27 GMT+0800 (中國標(biāo)準時間)
console.log(d.toLocaleDateString()); // 2023/9/19
console.log(d.toLocaleTimeString()); // 上午11:27:56
console.log(d.toUTCString()); // Tue, 19 Sep 2023 03:28:11 GMT
get
類方法
Date對象提供了一系列g(shù)et*方法,用來
獲取
實例對象某個方面的值
實例的方法名 | 作用 |
---|---|
getTime() | 返回實例距離1970年1月1日00:00:00的毫秒數(shù),等同于valueOf方法 |
getYear() | 返回距離1900的年數(shù) |
getFullYear() | 返回四位的年份 |
getMonth() | 返回月份(0 ~ 11,0表示1月,11表示12月) |
getDay() | 返回星期幾,星期日為0,星期一為1,以此類推 |
getDate() | 返回實例對象對應(yīng)每個月的幾號(從1開始) |
getHours() | 返回小時數(shù) (0 ~ 23) |
getMinutes() | 返回分鐘數(shù) (0 ~ 59) |
getSeconds() | 返回秒數(shù) (0 ~ 59) |
getMilliseconds() | 返回毫秒數(shù) (0 ~ 999) |
set
類方法
Date
對象提供了一系列set*
方法,用來設(shè)置
實例對象的各個方面。
實例的方法名 | 作用 |
---|---|
setTime(milliseconds) | 設(shè)置毫秒時間戳 |
setYear(year) | 設(shè)置距離1900年的年數(shù) |
setFullYear(year [, month, date]) | 設(shè)置四位年份 |
setMonth(month [, date]) | 設(shè)置月份(0-11) |
setDate(date) | 設(shè)置實例對象對應(yīng)的每個月的幾號(1-31),返回改變后毫秒時間戳 |
setHours(hour [, min, sec, ms]) | 設(shè)置小時(0-23) |
setMinutes(min [, sec, ms]) | 設(shè)置分鐘(0-59) |
setSeconds(sec [, ms]) | 設(shè)置秒(0-59) |
setMilliseconds() | 設(shè)置毫秒(0-999) |
date 相關(guān)工具方法
date 獲取當(dāng)天零點時間
參考文章@yujin0213
//獲取當(dāng)天零點的時間
const stamp1 = new Date(new Date().setHours(0, 0, 0, 0));
//獲取當(dāng)天23:59:59的時間
const stamp2 = new Date(new Date().setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000 - 1
);