日期处理类封装

/*
* 日期处理类封装
* 标准日期、中国标准时间、时间戳、毫秒数-时间戳*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
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值