JavaScript中的 Date(日期)對象及使用示例
一、Date對象
JavaScript中內(nèi)置了一個Date對象,它非常強(qiáng)大,提供了很多屬性和方法,用來創(chuàng)建、操作、格式化日期和時間,也可以進(jìn)行各種日期和時間的相關(guān)操作;
1、創(chuàng)建日期
使用new Date() 構(gòu)造函數(shù)來創(chuàng)建Date實(shí)例對象;
(1)常用方法
| 序號 | 方法 | 說明 |
|---|---|---|
| 1 | new Date() | 根據(jù)當(dāng)前時間創(chuàng)建Date對象; |
| 2 | new Date(milliseconds) | 根據(jù)milliseconds(13位時間戳)創(chuàng)建Date對象; |
| 3 | new Date(dateString) | 根據(jù)dateString(如:"2024/2/20 11:11:00")日期字符串創(chuàng)建Date對象; |
| 4 | new Date(year, month [, day, hours, minutes, seconds, milliseconds] ) | 根據(jù)傳入的年,月,日,時,分,秒,毫秒創(chuàng)建,前兩個參數(shù)必選; |
(2)使用示例
// 1、不傳參數(shù),根據(jù)當(dāng)前日期創(chuàng)建
console.log("根據(jù)當(dāng)前日期創(chuàng)建:", new Date());
// 2、傳入時間戳創(chuàng)建
console.log("根據(jù)時間戳創(chuàng)建:", new Date(1719363544868));
// 3、傳入日期字符串創(chuàng)建
console.log("根據(jù)日期字符串:", new Date("2014/01/08 11:11:00"));
// 4、傳入年,月,日,時,分,秒,毫秒創(chuàng)建
console.log("根據(jù)年,月,日,時,分,秒,毫秒創(chuàng)建:", new Date(2014, 2, 20, 11, 30, 23));

2、獲取日期
(1)常用方法
| 序號 | 方法 | 描述 |
|---|---|---|
| 1 | getFullYear() | 獲取4位數(shù)年份 |
| 2 | getMonth() | 獲取月份(0-11,0代表一月) |
| 3 | getDate() | 獲取月份中的某一天(1-31) |
| 4 | getHours() | 獲取小時數(shù)(0-23) |
| 5 | getMinutes() | 獲取分鐘數(shù)(0-59) |
| 6 | getSeconds() | 獲取秒數(shù)(0-59) |
| 7 | getMilliseconds() | 獲取毫秒數(shù)(0-999) |
| 8 | getTime() | 獲取自1970年1月1日以來的毫秒數(shù) |
| 9 | getDay() | 獲取星期中的某一天(0-6,0代表周日) |
(2)使用示例
// 創(chuàng)建當(dāng)前時間的日期對象
const date = new Date("2014-01-08 14:05:06");
console.log("新創(chuàng)建的Date對象:", date);
// 1、獲取4位數(shù)年份
console.log("getFullYear()方法,獲取4位數(shù)年份:", date.getFullYear());
// 2、獲取月份(0-11,0代表一月)
console.log("getMonth()方法,獲取月份:", date.getMonth() + 1);
// 3、獲取月份中的某一天(1-31)
console.log("getDate()方法,獲取月份中的某一天:", date.getDate());
// 4、獲取小時數(shù)(0-23)
console.log("getHours()方法,獲取小時數(shù):", date.getHours());
// 5、獲取分鐘數(shù)(0-59)
console.log("getMinutes()方法,獲取分鐘數(shù):", date.getMinutes());
// 6、獲取秒數(shù)(0-59)
console.log("getSeconds()方法,獲取秒數(shù):", date.getSeconds());
// 7、獲取毫秒數(shù)(0-999)
console.log("getMilliseconds()方法,獲取毫秒數(shù):", date.getMilliseconds());
// 8、獲取自1970年1月1日以來的毫秒數(shù)
console.log("getTime()方法,獲取自1970年1月1日以來的毫秒數(shù):", date.getTime());
// 9、獲取星期中的某一天(0-6,0代表周日)
console.log("getDay()方法,獲取星期中的某一天:", date.getDay());
3、設(shè)置日期
(1)常用方法
| 序號 | 方法 | 描述 |
|---|---|---|
| 1 | setFullYear() | 設(shè)置4位數(shù)年份 |
| 2 | setMonth() | 設(shè)置月份(0-11,0代表一月) |
| 3 | setDate() | 設(shè)置月份中的某一天(1-31) |
| 4 | setHours() | 設(shè)置小時數(shù)(0-23) |
| 5 | setMinutes() | 設(shè)置分鐘數(shù)(0-59) |
| 6 | setSeconds() | 設(shè)置秒數(shù)(0-59) |
| 7 | setMilliseconds() | 設(shè)置毫秒數(shù)(0-999) |
(2)使用示例
let date = new Date();
console.log("新創(chuàng)建的Date對象:", date);
// 1、設(shè)置4位數(shù)年份
date.setFullYear(2020);
console.log("setFullYear()方法,設(shè)置4位數(shù)年份:", date);
// 2、設(shè)置月份(0-11,0代表一月)
date.setMonth(10);
console.log("setMonth()方法,設(shè)置月份:", date);
// 3、設(shè)置月份中的某一天(1-31)
date.setDate(7);
console.log("setDate()方法,設(shè)置月份中的某一天:", date);
// 4、設(shè)置小時數(shù)(0-23)
date.setHours(12);
console.log("setHours()方法,設(shè)置小時數(shù):", date);
// 5、設(shè)置分鐘數(shù)(0-59)
date.setMinutes(24);
console.log("setMinutes()方法,設(shè)置分鐘數(shù):", date);
// 6、設(shè)置秒數(shù)(0-59)
date.setSeconds(48);
console.log("setSeconds()方法,設(shè)置秒數(shù):", date);
// 7、設(shè)置毫秒數(shù)(0-999)
date.setMilliseconds(123);
console.log("setMilliseconds()方法,設(shè)置毫秒數(shù):", date);
console.log("設(shè)置完成后的日期:", date);
4、轉(zhuǎn)換日期
(1)常用方法
| 序號 | 方法 | 描述 |
|---|---|---|
| 1 | toString() | 把 Date 對象轉(zhuǎn)換為字符串; |
| 2 | toDateString() | 把 Date 對象的日期部分轉(zhuǎn)換為字符串; |
| 3 | toTimeString() | 把 Date 對象的時間部分轉(zhuǎn)換為字符串; |
| 4 | toLocaleString() | 把 Date 對象,轉(zhuǎn)換為本地時間格式的字符串; |
| 5 | toLocaleDateString() | 把 Date 對象的日期部分,轉(zhuǎn)換為本地時間格式的字符串; |
| 6 | toLocaleTimeString() | 把 Date 對象的時間部分,轉(zhuǎn)換為本地時間格式的字符串; |
| 7 | toJSON() | 把 Date 對象轉(zhuǎn)換為JSON 數(shù)據(jù)格式字符串; |
8 | toISOString() | 把 Date 對象轉(zhuǎn)換為 ISO 標(biāo)準(zhǔn)的日期格式; |
(2)使用示例
let date = new Date("2014-01-08 14:05:06");
console.log("新創(chuàng)建的Date對象:", date);
// 1、把 Date 對象轉(zhuǎn)換為字符串;
console.log("toString()方法,轉(zhuǎn)換為字符串:", date.toString());
// 2、把 Date 對象的日期部分轉(zhuǎn)換為字符串;
console.log("toDateString()方法,日期部分轉(zhuǎn)換為字符串:", date.toDateString());
// 3、把 Date 對象的時間部分轉(zhuǎn)換為字符串;
console.log("toTimeString()方法,時間部分轉(zhuǎn)換為字符串:", date.toTimeString());
// 4、把 Date 對象,轉(zhuǎn)換為本地時間格式的字符串;
console.log("toLocaleString()方法,轉(zhuǎn)換為本地時間格式的字符串:", date.toLocaleString());
// 5、把 Date 對象的日期部分,轉(zhuǎn)換為本地時間格式的字符串;
console.log("toLocaleDateString()方法,日期部分轉(zhuǎn)換為本地時間格式的字符串:", date.toLocaleDateString());
// 6、把 Date 對象的時間部分,轉(zhuǎn)換為本地時間格式的字符串;
console.log("toLocaleTimeString()方法,時間部分,轉(zhuǎn)換為本地時間格式的字符串:", date.toLocaleTimeString());
// 7、把 Date 對象轉(zhuǎn)換為JSON 數(shù)據(jù)格式字符串;
console.log("toJSON()方法,轉(zhuǎn)換為JSON 數(shù)據(jù)格式字符串:", date.toJSON());
// 8、把 Date 對象轉(zhuǎn)換為 ISO 標(biāo)準(zhǔn)的日期格式;
console.log("toISOString()方法,轉(zhuǎn)換為 ISO 標(biāo)準(zhǔn)的日期格式:", date.toISOString());
5、獲取時間戳
(1)常用方法
| 序號 | 方法 | 說明 |
|---|---|---|
1 | Date.parse(new Date()) | 13位,精確到秒; |
| 2 | Math.round(new Date()) | 13位,精確到毫秒; |
| 3 | (new Date()).valueOf() | 13位,精確到毫秒; |
| 4 | new Date().getTime() | 13位,精確到毫秒; |
| 5 | +new Date() | 13位,精確到毫秒; |
(2)使用示例
// 1、使用Date.parse()
console.log("使用Date.parse()方法獲取時間戳:", Date.parse(new Date()));
// 2、使用Math.round()
console.log("使用Math.round()方法獲取時間戳:", Math.round(new Date()));
// 3、使用.valueOf()()
console.log("使用.valueOf()方法獲取時間戳:", (new Date()).valueOf());
// 4、使用.getTime()
console.log("使用.getTime()方法獲取時間戳:", (new Date()).getTime());
// 5、使用+new Date()
console.log("使用+new Date()方法獲取時間戳:", +new Date());
二、格式化Date對象
不難發(fā)現(xiàn),使用new Date()創(chuàng)建出來的日期對象,格式并不是想要的;
下面封裝格式化日期的方法,供日常開發(fā)參考;
// 創(chuàng)建當(dāng)前時間的日期對象
const date = new Date("2014-01-08 14:05:06");
console.log("新創(chuàng)建的Date對象:", date);
console.log(format1(date));
console.log(format2(date));
console.log(format3(date));
console.log(format4(date));
console.log(format5(date));
console.log(format6(date));
console.log(format7(date));
// 1、輸出日期:y:M:d(不補(bǔ)0)
function format1(date){
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
return year + '-' + month + '-' + day;
}
// 2、輸出日期:yyyy-MM-dd(補(bǔ)0)
function format2(date){
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day;
return year + '-' + month + '-' + day;
}
// 3、輸出時間:H:m:s
function format3(date){
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
return hour + ':' + minute + ':' + second;
}
// 4、輸出時間:HH:mm:ss
function format4(date){
let hour = date.getHours();
let minute = date.getMinutes();
let second = date.getSeconds();
hour = hour < 10 ? "0" + hour : hour;
minute = minute < 10 ? "0" + minute : minute;
second = second < 10 ? "0" + second : second;
return hour + ':' + minute + ':' + second;
}
// 5、輸出完整日期:y-M-d H:m:s
function format5(date){
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}
// 6、輸出完整日期:yy-MM-dd HH:mm:ss
function format6(date){
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
let hour = date.getHours();
let minute = date.getMinutes();
let second = date.getSeconds();
month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day;
hour = hour < 10 ? "0" + hour : hour;
minute = minute < 10 ? "0" + minute : minute;
second = second < 10 ? "0" + second : second;
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}
// 7、輸出完整日期:yy-MM-dd HH:mm:ss week
function format7(date){
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
let hour = date.getHours();
let minute = date.getMinutes();
let second = date.getSeconds();
let week = ['日', '一', '二', '三', '四', '五', '六'][date.getDay()];
month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day;
hour = hour < 10 ? "0" + hour : hour;
minute = minute < 10 ? "0" + minute : minute;
second = second < 10 ? "0" + second : second;
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + ' 星期' + week;
}
到此這篇關(guān)于JavaScript中的 Date(日期)對象的文章就介紹到這了,更多相關(guān)js date對象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript 根據(jù)歌名獲取播放地址和歌詞內(nèi)容
在前幾天做在線聽歌的過程中,碰到了根據(jù)歌名獲取播放地址和LRC文件內(nèi)容的問題,今晚花了幾個小時把接口整理了一下2009-06-06
uniapp開發(fā)APP之強(qiáng)制更新和熱更新的實(shí)現(xiàn)
使用uni-app開發(fā),可將代碼編譯到iOS、Android、微信小程序等多個平臺,升級時也需考慮多平臺同步升級,下面這篇文章主要給大家介紹了關(guān)于uniapp開發(fā)APP之強(qiáng)制更新和熱更新的相關(guān)資料,需要的朋友可以參考下2022-12-12
JavaScript學(xué)習(xí)總結(jié)之正則的元字符和一些簡單的應(yīng)用
這篇文章主要介紹了JavaScript學(xué)習(xí)總結(jié)之正則的元字符和一些簡單的應(yīng)用,需要的朋友可以參考下2017-06-06
bootstrap基礎(chǔ)知識學(xué)習(xí)筆記
這篇文章主要針對bootstrap基礎(chǔ)知識為大家整理了詳細(xì)的學(xué)習(xí)筆記,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
JS正則表達(dá)式替換字符串replace()方法實(shí)例代碼
正則表達(dá)式是用于匹配字符串中字符組合的模式,在js中正則表達(dá)式是對象,這篇文章主要給大家介紹了關(guān)于JS正則表達(dá)式替換字符串replace()方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
JavaScript中三種觀察者實(shí)現(xiàn)案例分享
前面突然看到 Object.defineProperty,就順道想到 Proxy,然后就想到了觀察者案例,這邊還沒有用 javascript編寫一個觀察者的案例呢,順道加入了一個 event-bus 監(jiān)聽事件案例,湊一起看一看不同的實(shí)現(xiàn)方式,需要的朋友可以參考下2023-08-08

