JavaScript中時間日期函數(shù)new?Date()詳解(5種獲取時間戳的函數(shù))
簡介:
JavaScript 中的 new Date() 方法用于創(chuàng)建一個新的 Date 對象,該對象表示當前日期和時間。Date 對象提供了許多方法和屬性,可以用于獲取和設(shè)置日期和時間信息。
new Date([year, month, day, hour, minute, second, millisecond])
其中,每個參數(shù)都是可選的。如果沒有指定參數(shù),則 new Date() 方法將創(chuàng)建一個表示當前日期和時間的 Date 對象。
一、關(guān)于new Date()函數(shù);
首先創(chuàng)建一個表示當前時間的 Date 對象
var nowTime = new Date();
console.log(nowTime);
控制臺輸出 Fri Nov 17 2023 10:41:58 GMT+0800 (中國標準時間)
//獲取 Date 當前時間 年月日時分秒
console.log(nowTime.toLocaleString());
//控制臺輸出 2023/11/17 10:41:58
//獲取 Date 當前時間 年月日
console.log(nowTime.toLocaleDateString());
//控制臺輸出 2023/11/17
//獲取 Date 當前時間 時分秒
console.log(nowTime.toLocaleTimeString());
//控制臺輸出 10:41:58
//獲取 Date 對象的年份
console.log(nowTime.getFullYear());
//控制臺輸出 2023
//獲取 Date 對象的月份(注意月份從0開始,所以使用時需要加1)
console.log(nowTime.getMonth());
//控制臺輸出 11
//獲取 Date 對象的日期
console.log(nowTime.getDate());
//控制臺輸出 17
//獲取 Date 對象的小時
console.log(nowTime.getHours());
//控制臺輸出 10
//獲取 Date 對象的分鐘
console.log(nowTime.getMinutes());
//控制臺輸出 41
//獲取 Date 對象的秒
console.log(nowTime.getSeconds());
//控制臺輸出 58
//獲取 Date 對象的毫秒
console.log(nowTime.getMilliseconds());
//控制臺輸出 917二、獲取 Date 對象時間戳的五種方式;
1、new Date().getTime()方法
const timestamp1 = new Date().getTime();
console.log(timestamp1);
//控制臺輸出 1700189060045
2、Date.now()方法
const timestamp2 = Date.now();
console.log(timestamp2);
//控制臺輸出 1700189060045
3、Date.parse(new Date())方法
const timestamp3 = Date.parse(new Date());
console.log(timestamp3);
//控制臺輸出 1700189060045
4、new Date()).valueOf()方法
const timestamp4 = new Date().valueOf();
console.log(timestamp4);
//控制臺輸出 1700189060045
5、Number(new Date())方法
const timestamp5 = Number(new Date());
console.log(timestamp5);
//控制臺輸出 1700189060045三、Date 對象應(yīng)用實例
實例1:計算月份差
有時候,我們需要知道兩個日期之間相差多少天。
可以先將日期轉(zhuǎn)換為毫秒數(shù),然后相減并除以一天的毫秒數(shù)(1000 * 60 * 60 * 24)來得到結(jié)果。
function daysBetween(date1, date2) {
let oneDay = 1000 * 60 * 60 * 24;
return Math.round((date2 - date1) / oneDay);
}
let start = new Date('June 20, 2022');
let end = new Date('July 20, 2022');
console.log(daysBetween(start, end)); // 輸出 "30"實例2:計算兩個日期之間的時間差
const startDate = new Date('2021-01-01');
const endDate = new Date('2021-12-31');
const timeDiff = endDate - startDate;
// 計算兩個日期之間的天數(shù)
const daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
// 計算兩個日期之間的小時數(shù)
const hoursDiff = Math.floor(timeDiff / (1000 * 60 * 60));
// 計算兩個日期之間的分鐘數(shù)
const minutesDiff = Math.floor(timeDiff / (1000 * 60));
// 計算兩個日期之間的秒數(shù)
const secondsDiff = Math.floor(timeDiff / 1000);
、、與實例1大同小異、、、、實例3:設(shè)置特定日期和時間:
const date = new Date(); // 設(shè)置日期為 2022 年 1 月 1 日 date.setFullYear(2022); date.setMonth(0); date.setDate(1); // 設(shè)置時間為 0 時 0 分 0 秒 date.setHours(0); date.setMinutes(0); date.setSeconds(0); // 獲取設(shè)置后的日期和時間 const formattedDate1 = date.toDateString(); const formattedTime2 = date.toTimeString();
實例4:獲取日期元素
在很多場景下,我們需要單獨獲取日期的年、月、日等元素。
我們可以使用getFullYear()、getMonth()、getDate()等方法來實現(xiàn)這一點。
let now = new Date();
let year = now.getFullYear(); // 獲取四位數(shù)年份
let month = now.getMonth(); // 注意月份是從0開始計數(shù)的,所以需要加1
let day = now.getDate();
console.log(`${year}/${month + 1}/${day}`); // 輸出類似 "2022/6/20"實例5:格式化日期
為了美觀或適應(yīng)用戶偏好,我們通常需要將日期格式化成特定的字符串。
這里我們使用模板字面量和padStart()方法來實現(xiàn):
function formatDate(date) {
let year = date.getFullYear();
let month = `0${date.getMonth() + 1}`.slice(-2);
let day = `0${date.getDate()}`.slice(-2);
return `${year}-${month}-${day}`;
}
let today = new Date();
console.log(formatDate(today)); // 輸出類似 "2022-06-20"附:日常方法
入?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
總結(jié)
到此這篇關(guān)于JavaScript中時間日期函數(shù)new Date()的文章就介紹到這了,更多相關(guān)JS時間日期函數(shù)new Date()內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js實現(xiàn)單層數(shù)組轉(zhuǎn)多層樹
這篇文章主要介紹了js實現(xiàn)單層數(shù)組轉(zhuǎn)多層樹方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
關(guān)于JavaScript 數(shù)組你應(yīng)該知道的事情(推薦)
這篇文章主要介紹了JavaScript 數(shù)組,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
javascript中的 object 和 function小結(jié)
JavaScript的面向?qū)ο笫腔谠蔚模袑ο蠖加幸粭l屬于自己的原型鏈。Object與Function可能很多看Object instanceof Function , Function instanceof Object都為true而迷惑,所以首先看下對象的實例。2016-08-08
JavaScript自定義Webpack配置實現(xiàn)流程介紹
本系列主要整理前端面試中需要掌握的知識點。本節(jié)介紹webpack如何優(yōu)化前端性能,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-10-10
JavaScript與ActionScript3兩者的同性與差異性
接觸JavaScript和ActionScript3也有近5年的時間了,它們都是應(yīng)用比較廣泛的腳本語言.接下來通過本文給大家介紹JavaScript與ActionScript3兩者的同性與差異性,感興趣的朋友一起學(xué)習(xí)吧2016-09-09

