/*
* 日期处理类封装
* 标准日期、中国标准时间、时间戳、毫秒数-时间戳*1000就是毫秒数
*/
export function DateFactory(date = new Date()){
this.date = new Date(date)
this.year = this.date.getFullYear()
this.month = this.date.getMonth()
this.day = this.date.getDate()
this.hours = this.date.getHours()
this.minutes = this.date.getMinutes()
this.seconds = this.date.getSeconds()
this.weekDay = this.date.getDay()
}
Object.assign(DateFactory.prototype, {
/*
* (中国标准时间/时间戳格)转换为标准日期
* @precise true/false 是否精确时分秒
* @timestamp (a.时间戳:1619508060000、b.日期格式:Tue Apr 27 2021 15:21:00 GMT+0800 (中国标准时间))
* return yyyy-MM-dd HH:mm:ss
* */
dateFormat(precise = true,timestamp) {
if(timestamp) this.constructor(timestamp)
else this.constructor()
let MM = this.month + 1 < 10 ? "0"+(this.month + 1) : this.month + 1
let dd = this.day < 10 ? "0" + this.day : this.day
let HH = this.hours < 10 ? "0" + this.hours : this.hours
let mm = this.minutes < 10 ? "0" + this.minutes : this.minutes
let ss = this.seconds < 10 ? "0" + this.seconds : this.seconds
let formatDate = this.year + "-" + MM + "-" + dd
return precise ? formatDate + " "+ HH + ":" + mm + ":" + ss : formatDate
},
/*
* 时间戳转换
* @date (yyyy-MM-dd HH:mm:ss / yyyy/MM/dd HH:mm:ss)
* retrun 时间戳
* */
changeTimestamp(date){
this.constructor(date)
return this.date.getTime()
},
/*
* 日期分割符-替换/
* @date yyyy-MM-dd HH:mm:ss
* return yyyy/MM/dd HH:mm:ss
* */
separator(date){ return date.replace(/-/g,'/') },
/*
* 获取n小时前后日期时间
* @days yyyy-MM-dd HH:mm:ss
* @hours n小时 -1=前个1小时 0=当前小时 1=下个1小时
* 返回格式yyyy-MM-dd HH:mm:ss
* */
getAfterHour(date = new Date() ,hours = 0 ){
this.constructor(date)
return this.dateFormat(true,this.date.setHours(this.date.getHours() + hours))
},
/*
* 获取n天数前后日期时间
* @date yyyy-MM-dd HH:mm:ss
* @daysNum n天数前后 -1前天 0当天 1明天
* @precise 是否精确到时分秒
* return yyyy-MM-dd HH:mm:ss
* */
getAfterDay(date = new Date(),daysNum = 0,precise ){
this.constructor(date)
return this.dateFormat(precise,this.date.setDate(this.date.getDate() + daysNum))
},
/*
* 获取n月数前后日期时间
* @date yyyy-MM-dd HH:mm:ss
* @monthNum n月数前后 -1前1月 0当月 1下1月
* @precise 是否精确到时分秒
* return yyyy-MM-dd HH:mm:ss
* */
getAfterMonth(date = new Date(),monthNum = 0,precise){
this.constructor(date)
return this.dateFormat(precise,this.date.setMonth(this.date.getMonth() + monthNum))
},
/*
* 获取第n周的(周一、周日)日期
* @date yyyy-MM-dd
* @weekNum n周数前后 -1前1周 0当周 1下1周
* return [周一日期,周日日期]
* */
getWeekDate(date = new Date(), weekNum = 0) {
this.constructor(date)
// 周一日期
let startDate = this.getAfterDay(date, this.weekDay === 0 ? -6 : -(this.weekDay-1),false)
if(weekNum) startDate = this.getAfterDay(startDate,weekNum*7,false)
// 周日日期
let endDate = this.getAfterDay(startDate,6,false)
return [startDate,endDate]
},
/*
* 获取第n月的(开始、结束)日期
* @date yyyy-MM
* @monthNum n月数前后 -1前1月 0当月 1下1月
* return {date:[开始日期,结束日期],daysNum:月份天数}
* */
getMonthDate(date,monthNum = 0){
let startDate = this.getAfterMonth(date + "-01",monthNum,false)
this.constructor(startDate)
let daysNum = new Date(this.year,this.month+1,0).getDate()
let endDate = this.getAfterMonth(date + "-" + daysNum,monthNum,false)
return {date:[startDate,endDate],daysNum}
},
/*
* 获取指定区间日期中全部日期枚举
* @startDate 开始日期 yyyy-MM-dd
* @endDate 结束日期 yyyy-MM-dd
* return 数组
* */
getDateRange(startDate, endDate) {
let dateRange = [];
let i = 0;
while(startDate <= endDate){
dateRange[i] = startDate;
let nextDate = this.changeTimestamp(startDate) + ( 24 * 60 * 60 * 1000);
startDate = this.dateFormat(false,nextDate)
i++;
}
return dateRange;
}
})
export const dateFactory = new DateFactory()
export default {
dateFactory
}
日期处理类封装
于 2021-07-04 23:37:02 首次发布