一、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)