眾所周知,JavaScript核心包含Data()構造函數,用來創建表示時間和日期的對象。
今天主要跟大家梳理一下,常用的時間、日期處理方法,方便大家使用和理解
格式化時間
老生常談,大概會這么寫
1 2 3 4 5 6 7 8 9 10 11 |
var format = function (time) { var y = time.getFullYear(); var M = time.getMonth() + 1; var d = time.getDate(); var h = time.getHours(); var m = time.getMinutes(); var s = time.getSeconds(); return y + '-' + M + '-' + d + ' ' + h + ':' + m + ':' + s; }
var time1 = format(new Date()); |
但是有什么問題呢?一般來說小於10的值,要在前面添加字符串‘0’的,我們大可以寫個判斷來解決他,但是太麻煩了~
其實可以這樣
1 2 3 4 5 6 7 8 |
var format = function (time) { var date = new Date(+time + 8 * 3600 * 1000); return date.toJSON().substr(0, 19).replace('T', ' ').replace(/-/g, '.'); } var time1 = format(new Date());
|
獲取當月最后一天
一個月可能有28/29/30/31天,使用寫死數據的方式來解決閏年和大小月顯然是不科學的。
1 2 3 4 5 6 7 8 |
function getLastDayOfMonth (time) { var month = time.getMonth(); time.setMonth(month+1); time.setDate(0); return time.getDate() } getLastDayOfMonth(new Date())
|
獲取這個季度第一天
用來確定當前季度的開始時間,常用在報表中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function getFirstDayOfSeason (time) { var month = time.getMonth(); if(month <3 ){ time.setMonth(0); }else if(2 < month && month < 6){ time.setMonth(3); }else if(5 < month && month < 9){ time.setMonth(6); }else if(8 < month && month < 11){ date.setMonth(9); } time.setDate(1); return time; } getFirstDayOfSeason(new Date())
|
獲取中文星期
這也是個比較常見的雪球,完全沒必要寫一長串switch啦,直接用charAt來解決。
1 |
let time ="日一二三四五六".charAt(new Date().getDay()); |
獲取今天是當年的第幾天
來看看今年自己已經浪費了多少時光~
1 2 3 4 |
var time1 = Math.ceil(( new Date() - new Date(new Date().getFullYear().toString()))/(24*60*60*1000));
|
獲取今天是當年的第幾周
日歷、表單常用
1 2 3 |
var week = Math.ceil(((new Date() - new Date(new Date().getFullYear().toString()))/(24*60*60*1000))/7);
|
獲取今天是當年還剩多少天
再來看看今年還有多少天可以浪費~
1 2 3 4 5 6 7 8 9 |
function restOfYear(time) { var nextyear = (time.getFullYear() + 1).toString(); var lastday = new Date(new Date(nextyear)-1); console.log(lastday) var diff = lastday - time; return Math.floor(diff / (1000 * 60 * 60 * 24)); } restOfYear(new Data())
|
計算兩個時間的間隔
可以拓展為倒計時、有效期等用途
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
function diffTime(startDate,endDate,location) { var diff=new Date(endDate).getTime() - startDate;
var days=Math.floor(diff/(24*3600*1000));
var leave1=diff%(24*3600*1000); var hours=Math.floor(leave1/(3600*1000));
var leave2=leave1%(3600*1000); var minutes=Math.floor(leave2/(60*1000));
var leave3=leave2%(60*1000); var seconds=Math.round(leave3/1000); if(location === "Day") { return returnStr = "還有" + days + "天"; }else if(location === "Hours") { return returnStr = "還有" + (hours+days*24) + "小時"; }else if(location === "Minutes") { return returnStr = "還有" + (minutes+(hours+days*24)*60) + "分鍾"; }else if(location === "Seconds") { return returnStr = "還有" + (seconds+(minutes+(hours+days*24)*60)*60) + "秒"; }else{ return returnStr = "還有" + days + "天" + hours + "小時" + minutes + "分鍾" + seconds + "秒"; } } console.log(diffTime(new Date(), '2019-8-19 16:00:00','Minutes'))
|
計算指定時間間隔前后的日期
可用於時間追溯、活動預告等用途
1 2 3 4 5 6 |
function GetDate(time,count) { time.setDate(time.getDate() + count); var date = new Date(+time + 8 * 3600 * 1000); return date.toJSON().substr(0, 19).replace('T', ' ').replace(/-/g, '.'); } GetDate(new Date(),100) |
計算當周開始和結束時間
很常見的需求,可用來做簽到等
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function getwholetWeek(now){ var weekday = now.getDay(); weekday = weekday === 0 ? 7 : weekday; var firstDay = GetDate(now,-weekday); var lastDay = GetDate(now,7 - 1); return { firstDay: firstDay, lastDay: lastDay }; }
console.log(getwholetWeek(new Date()))
|