JavaScript中Date對象的方法解析
上篇文章講到了JavaScript的Date對象,對Date對象的創建和屬性進行了講解,這篇博客我們來講一講Date對象的方法有哪些。
1.獲取日期的方法
// 獲取當前的日期
var oDate=new Date();
// 方法 描述
// getDate() 從 Date 對象返回一個月中的某一天 (1 ~ 31)。
console.log(oDate.getDate());
// getDay() 從 Date 對象返回一週中的某一天 (0 ~ 6)。
console.log(oDate.getDay());// 0代表週日
// getFullYear() 從 Date 對象以四位數字返回年份。
console.log(oDate.getFullYear());
// getMonth() 從 Date 對象返回月份 (0 ~ 11)。
console.log(oDate.getMonth());// 一月份 是 0
// getHours() 返回 Date 對象的小時 (0 ~ 23)。
console.log(oDate.getHours());
// getMinutes() 返回 Date 對象的分鐘 (0 ~ 59)。
console.log(oDate.getMinutes());
// getSeconds() 返回 Date 對象的秒數 (0 ~ 59)。
console.log(oDate.getSeconds());
// getMilliseconds() 返回 Date 對象的毫秒(0 ~ 999)。
console.log(oDate.getMilliseconds());
// getTime() 返回 1970 年 1 月 1 日至今的毫秒數。
console.log(oDate.getTime());
2.獲取世界日期
// 獲取世界時間的方法 格林威治 本初子午線
// getUTCDate() 根據世界時從 Date 對象返回月中的一天 (1 ~ 31)。
console.log(oDate.getUTCDate());
// getUTCDay() 根據世界時從 Date 對象返回週中的一天 (0 ~ 6)。
console.log(oDate.getUTCDay());
// getUTCFullYear() 根據世界時從 Date 對象返回四位數的年份。
// getUTCMonth() 根據世界時從 Date 對象返回月份 (0 ~ 11)。
// getUTCHours() 根據世界時返回 Date 對象的小時 (0 ~ 23)。
console.log(oDate.getUTCHours());
// getUTCMinutes() 根據世界時返回 Date 對象的分鐘 (0 ~ 59)。
// getUTCSeconds() 根據世界時返回 Date 對象的秒鐘 (0 ~ 59)。
// getUTCMilliseconds() 根據世界時返回 Date 對象的毫秒(0 ~ 999)。
3.設置日期
// setDate() 設置 Date 對象中月的某一天 (1 ~ 31)。
oDate.setDate(13);
console.log(oDate);
// setFullYear() 設置 Date 對象中的年份(四位數字)。
// setHours() 設置 Date 對象中的小時 (0 ~ 23)。
// setMilliseconds() 設置 Date 對象中的毫秒 (0 ~ 999)。
// setMinutes() 設置 Date 對象中的分鐘 (0 ~ 59)。
// setMonth() 設置 Date 對象中月份 (0 ~ 11)。
oDate.setMonth(0);
console.log(oDate);
// setSeconds() 設置 Date 對象中的秒鐘 (0 ~ 59)。
// setTime() setTime() 方法以毫秒設置 Date 對象。
// setUTCDate() 根據世界時設置 Date 對象中月份的一天 (1 ~ 31)。
// setUTCFullYear() 根據世界時設置 Date 對象中的年份(四位數字)。
// setUTCHours() 根據世界時設置 Date 對象中的小時 (0 ~ 23)。
// setUTCMilliseconds() 根據世界時設置 Date 對象中的毫秒 (0 ~ 999)。
// setUTCMinutes() 根據世界時設置 Date 對象中的分鐘 (0 ~ 59)。
// setUTCMonth() 根據世界時設置 Date 對象中的月份 (0 ~ 11)。
// setUTCSeconds() setUTCSeconds() 方法用於根據世界時 (UTC) 設置指定時間的秒字段。
4.轉換爲字符串形式
// toString() 轉換爲字符串
console.log(oDate.toString());
// toDateString() 把 Date 對象的日期部分轉換爲字符串。
console.log(oDate.toDateString());
// toTimeString() 把 Date 對象的時間部分轉換爲字符串。
console.log(oDate.toTimeString());
// toUTCString() 根據世界時,把 Date 對象轉換爲字符串。
console.log(oDate.toUTCString());
// toLocaleString() 據本地時間格式,把 Date 對象轉換爲字符串。
console.log(oDate.toLocaleString());
// toLocaleDateString() 根據本地時間格式,把 Date 對象的日期部分轉換爲字符串。
console.log(oDate.toLocaleDateString());
// toLocaleTimeString() 根據本地時間格式,把 Date 對象的時間部分轉換爲字符串。
console.log(oDate.toLocaleTimeString());
// toJSON() 以 JSON 數據格式返回日期字符串。
5.其他方法
// getTimezoneOffset() 返回本地時間與格林威治標準時間 (GMT) 的分鐘差。
console.log(oDate.getTimezoneOffset());
// parse() 返回1970年1月1日午夜到指定日期(字符串)的毫秒數。
console.log(Date.parse("1970-1-2"));
// UTC() 根據世界時返回 1970 年 1 月 1 日 到指定日期的毫秒數。
console.log(Date.UTC(1970,1,2));
// valueOf() 返回Date對象的原始值
console.log(oDate.valueOf());
注意 :js中Date對象的方法看起來有很多,但實際上真正去記憶時發現只有10個左右,需要分清本地日期和世界日期的區別,然後就是 get 和 set 的區別。
視頻講解鏈接:
https://www.bilibili.com/video/BV1Wv411z7yd/