// 獲取當前日期
function getNowDate (){
let date =newDate()
let Y = date.getFullYear()
let M = date.getMonth()+1
let D = date.getDate()
M = M <10? `0${M}` : M
D = D <10? `0${D}` : D
return `${Y}-${M}-${D}`
}
獲取當前日期前n個月
// 獲取當前日期前n個月的日期
function getPreMonthDay (date, monthNum){
let dateArr = date.split('-')
let year = dateArr[0]// 獲取當前日期的年份
let month = dateArr[1]// 獲取當前日期的月份
let day = dateArr[2]// 獲取當前日期的日// let days = new Date(year, month, 0)// days = days.getDate() //獲取當前日期中月的天數
let year2 = year
let month2 =parseInt(month)- monthNum
if(month2 <=0){
year2 =parseInt(year2)-parseInt(month2 /12===0?1: Math.abs(parseInt(month2 /12))+1)
month2 =12-(Math.abs(month2)%12)}
let day2 = day
let days2 =newDate(year2, month2,0)
days2 = days2.getDate()if(day2 > days2){
day2 = days2
}if(month2 <10){
month2 ='0'+ month2
}
let t2 = year2 +'-'+ month2 +'-'+ day2
return t2
}
判斷是否爲合法日期
/**
* @method 判斷是否爲合法日期
* @param date 爲Date轉化後的值
*/
function isValidDate (date){return date instanceof Date &&!isNaN(date.getTime())}
判斷一個時間是否位於兩時間之間
/**
* @method 判斷一個時間是否位於兩時間之間
* @param sDate1,sDate2爲日期,如:2019-01-01
*/
function dateBetweenJudget (startDate, endDate, date0){
let oStartDate =newDate(startDate)
let oEndDate =newDate(endDate)
let oDate0 =newDate(date0)if((oEndDate.getTime()>= oDate0.getTime())&&(oStartDate.getTime()<= oDate0.getTime())){returntrue}returnfalse}
比較兩個日期的大小
/* @method 比較兩個日期的大小
* @param sDate1,sDate2爲日期,如:2019-01-01
*/
function dateJudget (sDate1, sDate2){
var oDate1 =newDate(sDate1)
var oDate2 =newDate(sDate2)if(oDate1.getTime()> oDate2.getTime()){returnfalse}returntrue}
獲取當前月份前n個月或者後n個月(返回日期列表)
/**
* @method 獲取當前月份前n個月或者後n個月
* @param range 範圍,before表示前n個月,after表示後n個月
* @param date 特定的日期,如2019-01-01,若傳‘’,默認爲當前日期
*/
function getMonthRange (range, n, date =''){
var monthArr =[]
let data =(date ==='')?newDate():newDate(date)if(range ==='before'){
data.setMonth(data.getMonth()+1,1)// 獲取到當前月份,設置月份}else{
data.setMonth(data.getMonth()-1,1)// 獲取到當前月份,設置月份}for(var i =0; i < n; i++){if(range ==='before'){
data.setMonth(data.getMonth()-1)// 每次循環一次 月份值減1}else{
data.setMonth(data.getMonth()+1)// 每次循環一次 月份值加1}
let m = data.getMonth()+1
let m2 = m <10?'0'+ m : m
let json ={
yearAndMonth: data.getFullYear()+'-'+ m2,
month: m,
year: data.getFullYear(),}
monthArr.push(json)}return monthArr
}
獲取某個月有多少天
/**
* @method 獲取某個月有多少天數
* @param year 年
* @param month 月(如:2)
*/
function getDaysInMonth (year, month){returnnewDate(year, month,0).getDate()}
獲取當前日期的前一個月
/**
* @method 獲取當前日期的前一個月
*/
function getPreMonthDate (times){
times =newDate(times)
let Y = times.getFullYear()
let M = times.getMonth()+1const D = times.getDate()if(M ===1){
M =12
Y = Y -1}else{
M = M -1}return `${Y}-${M}-${D}`
}
獲取當前日期的前兩個月
/**
* @method 獲取當前日期的前兩個月
*/
function getPreMonthDate2 (times){
times =newDate(times)
let Y = times.getFullYear()
let M = times.getMonth()+1const D = times.getDate()if(M ===2){
M =12
Y = Y -1}elseif(M ===1){
M =11
Y = Y -1}else{
M = M -2}return `${Y}-${M}-${D}`
}
根據日期判斷今天周幾
/**
* @method 根據日期判斷今天周幾
* @param {*} date 日期格式爲字符串2018-01-01
*/
function getDateWeek (date){const weekDay =['週日','週一','週二','週三','週四','週五','週六']const myDate =newDate(Date.parse(date))return weekDay[myDate.getDay()]}