一、Date類型
在講述常見日期問題之前,先梳理一下Date類型的方法。
ECMAScript中的Date類型使用自UTC(Coordinated in Universal Time,國際協調時間)1970年1月1日午夜(零時)開始經過的毫秒數來儲存日期。
常用方法清單:
方法 | 描述 |
---|---|
Date() | 傳回當日的日期和時間。 |
getDate() | 從 Date 對象傳回一個月中的某一天 (1 ~ 31)。 |
getDay() | 從 Date 對象傳回一周中的某一天 (0 ~ 6)。 |
getMonth() | 從 Date 對象傳回月份 (0 ~ 11)。 |
getFullYear() | 從 Date 對象以四位數字傳回年份。 |
getHours() | 傳回 Date 對象的小時 (0 ~ 23)。 |
getMinutes() | 傳回 Date 對象的分鐘 (0 ~ 59)。 |
getSeconds() | 傳回 Date 對象的秒數 (0 ~ 59)。 |
getMilliseconds() | 傳回 Date 對象的毫秒(0 ~ 999)。 |
getTime() | 傳回 1970 年 1 月 1 日至今的毫秒數。 |
getTimezoneOffset() | 傳回本地時間與格林威治标準時間 (GMT) 的分鐘差。 |
parse() | 傳回1970年1月1日午夜到指定日期(字元串)的毫秒數。 |
setDate() | 設定 Date 對象中月的某一天 (1 ~ 31)。 |
setMonth() | 設定 Date 對象中月份 (0 ~ 11)。 |
setFullYear() | 設定 Date 對象中的年份(四位數字)。 |
setHours() | 設定 Date 對象中的小時 (0 ~ 23)。 |
setMinutes() | 設定 Date 對象中的分鐘 (0 ~ 59)。 |
setSeconds() | 設定 Date 對象中的秒鐘 (0 ~ 59)。 |
setMilliseconds() | 設定 Date 對象中的毫秒 (0 ~ 999)。 |
setTime() | 以毫秒設定 Date 對象。 |
toSource() | 傳回該對象的源代碼。 |
toString() | 把 Date 對象轉換為字元串。 |
toTimeString() | 把 Date 對象的時間部分轉換為字元串。 |
toDateString() | 把 Date 對象的日期部分轉換為字元串。 |
toUTCString() | 根據世界時,把 Date 對象轉換為字元串。 |
toLocaleString() | 根據本地時間格式,把 Date 對象轉換為字元串。 |
toLocaleTimeString() | 根據本地時間格式,把 Date 對象的時間部分轉換為字元串。 |
toLocaleDateString() | 根據本地時間格式,把 Date 對象的日期部分轉換為字元串。 |
UTC() | 根據世界時傳回 1970 年 1 月 1 日 到指定日期的毫秒數。 |
valueOf() | 傳回 Date 對象的原始值。 |
補充:
1. 格林威治時間是指位于英國倫敦郊區的皇家格林尼治天文台的标準時間,因為本初子午線被定義在通過那裡的經線。
new Date().getTimezoneOffset() / 60; // -8,即英國的當地時間比中國的中原標準時間晚8小時
2. 可以通過getUTCMonth、setUTCMonth等方法設定世界時的年、月、日、時、分、秒、毫秒。
3. 把Date對象轉化為字元串
new Date().toString(); // "Fri Aug 05 2016 11:54:25 GMT+0800 (CST)"
new Date().toDateString() // "Fri Aug 05 2016"
new Date().toTimeString() // "11:54:48 GMT+0800 (CST)"
4. 擷取指定時間毫秒
// 2016年8月5日
Date.parse('08/05/2016'); // 1470326400000
new Date('08/05/2016').getTime(); // 1470326400000
Date.UTC(2016, 7, 5); // 1470355200000
UTC()方法中,月份從0開始且獲得的毫秒值是世界時(即需要+8小時)
二、擷取過去第n天的時間
/*
* 擷取過去的n天
* @param data 過去的天數
* @param date 指定日期
*/
function getBeforeDay(data, date) {
var date = date || new Date(),
timezone = "+08:00"; // 時區
var now = setTimezone.call(date, timezone.replace(":",".")); // 擷取指定時區的目前日期
var beforeDay = new Date(Date.parse(now.toString()) - 86400000 * data);
return format.call(beforeDay, "yyyy/MM/dd"); // 格式化日期
}
/**
* 設定時區
* @param tzn
* @returns {setTimezone}
*/
function setTimezone(tzn) {
// 傳回指定日期與格林威治标準時間 (GMT) 的分鐘差[注意,東時區為負值]
tzn = tzn * 60 * -1;
// 目前時間-相差毫秒數[注意,東時區為負值]
this.setTime(this.getTime() - (tzn - this.getTimezoneOffset()) * 60 * 1000);
return this;
}
/**
* 日期格式化
* @param format
* @returns {*}
*/
function format (format) {
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds() //millisecond
};
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}
三、擷取指定月份的天數
方式一:月曆字典表
/**
* 擷取指定月份的天數
* 像月份、星期這樣可列舉且不易發生改變、資料項不是很大的,建議使用字典直接展現出來!!
* @param year 年份,如:2016
* @param month 月份,如:0(注意,遵循預設月曆,從0開始)
*/
function getDaysInMonth (year, month) {
return [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
}
/**
* 判斷是否為瑞年
*/
function isLeapYear(year) {
return ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0);
}
方式二:通過月曆構造器
/**
* 擷取指定月份的天數
* @param year 年份,如:2016
* @param month 月份,如:0(注意,遵循預設月曆,從0開始)
*/
function getDaysInMonth (year, month) {
// 将天置為0,會擷取其上個月的最後一天
// 擷取1月份的天數
// new Date(2016, 2 , 0) ==> Mon Feb 29 2016 00:00:00 GMT+0800 (CST)
var date = new Date(year, month + 1, 0);
return date.getDate();
}
四、擷取上個周的開始時間(上周一)&結束時間(上周日)
方式一:擷取本周第一天,然後before(1)、before(7)
function getDayOfLastWeek(){
var weekday = new Date().getDay(); // 擷取目前是周幾(周日:0)
weekday = weekday === 0 ? 7 : weekday;
var firstDay = getBeforeDay(weekday + 7 -1);
var lastDay = getBeforeDay(weekday);
return {
lastWeekFirstDay: firstDay,
lastWeekLastDay: lastDay
};
}
五、擷取上個月的開始時間和結束時間
/**
* new Date(年, 月, 日) ==> 月份從0開始
*/
function getDayOfLastMonth(){
var date = new Date(),
currentMonth = date.getMonth();
return {
lastMonthFirstDay: format.call(new Date(date.getFullYear(), currentMonth - 1, 1), "yyyy/MM/dd"),
lastMonthLastDay: format.call(new Date(date.getFullYear(), currentMonth, 0), "yyyy/MM/dd")
}
}
六、格外注意
new Date(2016, 7, 32); // Thu Sep 01 2016 00:00:00 GMT+0800 (CST)
new Date(2016, 12, 1); // Sun Jan 01 2017 00:00:00 GMT+0800 (CST)
原創文章請随便轉載。願和大家分享,并且一起進步。-- 江 coder