时间样式的封装

时间样式的封装 支持各种格式

const WEEK_ARR = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
export class DateFormat {
  date
  constructor(date = new Date()) {
    this.date = this.getDate(date)
  }

  getTimestamp(days) {
    return days * 24 * 60 * 60 * 1000
  }

  add(count, type = 'day') {
    switch (type) {
      case 'day':
        this.date = new Date(+this.date + this.getTimestamp(count))
        break
      case 'week':
        this.date = new Date(+this.date + this.getTimestamp(count * 7))
        break
      case 'month':
        const thisMonth = this.date.getMonth()
        // const year = (thisMonth + count)/12 >> 0
        // const month = (thisMonth + count) % 12
        // this.date.setYear(this.date.getFullYear()+year)
        this.date.setMonth(thisMonth + count)
        break
      default:
    }
    return this
  }

  /*
   * @date  可以是时间戳,字符串,Date对象
   */
  getDate(date) {

    if (!date) {
      return new Date()
    }

    if (date instanceof Date) {
      return date
    }

    if (isNaN(date) && typeof date === 'string') {
      // 2017-01 || 2017/1
      if (/^\d{4}-\d{2}$/.test(date) || /^\d{4}\/\d{2}$/.test(date)) {
        date = `${date}-01`
      }
      return new Date(date.replace(/-/g, '/'))
    }

    if (typeof + date === 'number') {
      return new Date(date)
    }
  }

  isSame(time, type = 'day') {
    if (type === 'day') {
      return this.format('YYYY-MM-DD') === time.format('YYYY-MM-DD')
    } else if (type === 'month') {
      return this.format('YYYY-MM') === time.format('YYYY-MM')
    } else {
      return this.format('YYYY') === time.format('YYYY')
    }
  }

  //timestamp
  timestamp() {
    return +this.date
  }

  //本月第几天
  today() {
    return this.date.getDate()
  }

  //本周第几天
  day() {
    return this.date.getDay()
  }

  month() {
    return this.date.getMonth()
  }

  //当月有几天
  daysInMonth() {
    const date = new Date()
    const currMonth = this.date.getMonth()
    date.setMonth(currMonth + 1)
    date.setDate(0);
    return date.getDate();
  }

  getWeekString() {
    return WEEK_ARR[this.date.getDay()]
  }

  getWeekIndex() {
    return this.date.getDay()
  }

  format(format) {
    if (format.key == 2) {
      if (format.time) {
        let kong = ''
        let k = ""
        var d = new Date(format.time)
        if (d.getMonth() + 1 < 10) {
          kong = 0
        } else {
          kong = ''
        }
        if (d.getDate() < 10) {
          k = 0
        } else {
          k = ""
        }
        var datetime = `${d.getFullYear()}${kong}${(d.getMonth() + 1)}${k}${d.getDate()}`;
        return datetime
      } else {
        return ''
      }

    } else {
      const year = this.date.getFullYear()
      const month = this.date.getMonth() + 1
      const day = this.date.getDate()
      //月历考勤
      const signMonth = this.date.getMonth()
      const fullMonth = signMonth > 9 ? signMonth : signMonth == 0 ? 12 : `0${signMonth}`
      const hour = this.date.getHours()
      const minute = this.date.getMinutes()
      const second = this.date.getSeconds()
      const monthStr = month > 9 ? month : `0${month}`
      const dayStr = day > 9 ? day : `0${day}`
      const hourStr = hour > 9 ? hour : `0${hour}`
      const minuteStr = minute > 9 ? minute : `0${minute}`
      const secondStr = second > 9 ? second : `0${second}`
      let result = ''
      switch (format) {
        case 'YYYY':
          result = year
          break
        case 'MM':
          result = monthStr
          break
        case 'M':
          result = month
          break
        case 'DD':
          result = dayStr
          break
        case 'D':
          result = day
          break
        case 'HH':
          result = hourStr
          break
        case 'H':
          result = hour
          break
        case 'mm':
          result = minuteStr
          break
        case 'm':
          result = minute
          break
        case 'ss':
          result = secondStr
          break
        case 's':
          result = second
          break
        case 'HH:mm':
          result = `${hourStr}:${minuteStr}`
          break
        case 'HH:mm:ss':
          result = `${hourStr}:${minuteStr}:${secondStr}`
          break
          //6.12月历考勤新增
        case 'YYYY-MM-R':
          result = signMonth == 0 ? `${year - 1}-${fullMonth}` : `${year}-${fullMonth}`
          break
        case 'YYYYMM':
          result = `${year}${monthStr}`
          break
        case 'YYYYMMDD':
          result = `${year}${monthStr}${dayStr}`
          break
        case 'YYYY-MM':
          result = `${year}-${monthStr}`
          break
        case 'YYYY-MM-DD':
          result = `${year}-${monthStr}-${dayStr}`
          break
        case 'YYYY/MM/DD':
          result = `${year}/${monthStr}/${dayStr}`
          break
        case 'YYYY-M-D':
          result = `${year}-${month}-${day}`
          break
        case 'YYYY-MM-DD HH:mm':
          result = `${year}-${monthStr}-${dayStr} ${hourStr}:${minuteStr}`
          break
        case 'YYYY-MM-DD HH:mm:ss':
          result = `${year}-${monthStr}-${dayStr} ${hourStr}:${minuteStr}:${secondStr}`
          break
        case 'YYYY-M-D H:m:s':
          result = `${year}-${month}-${day} ${hour}:${minute}:${second}`
          break
        case 'YYYY-MM-DD 00:00:00':
          result = `${year}-${monthStr}-${dayStr} 00:00:00`
          break
        case 'MM月':
          result = `${monthStr}月`
          break
        case 'M月D日':
          result = `${month}${day}日`
          break
        case 'M.D':
          result = `${month}.${day}`
          break
        case 'MM.DD':
          result = `${monthStr}.${dayStr}`
          break
        case 'YYYY/MM/DD':
          result = `${year}/${monthStr}/${dayStr}`
          break
        case 'YYYY/MM/DD HH:mm':
          result = `${year}/${monthStr}/${dayStr} ${hourStr}:${minuteStr}`
          break
        case 'MM/DD HH:mm':
          result = `${monthStr}/${dayStr} ${hourStr}:${minuteStr}`
          break
        case 'MM月DD日':
          result = `${month}${day}日`
          break
        case 'YYYY年MM月DD日':
          result = `${year}${month}${day}日`
          break
        case 'YYYY年MM月':
          result = `${year}${monthStr}月`
          break
        case 'MM月DD日 dddd':
          result = `${monthStr}${dayStr}${this.getWeekString()}`
          break
        case 'YYYY年MM月DD日 dddd':
          result = `${year}${monthStr}${dayStr}${this.getWeekString()}`
          break
        case 'MM月DD日 HH:mm':
          result = `${monthStr}${dayStr}${hourStr}:${minuteStr}`
          break
        case 'YYYY年MM月DD日 HH:mm':
          result = `${year}${monthStr}${dayStr}${hourStr}:${minuteStr}`
          break
        case 'YYYY年MM月DD日 HH:mm:ss':
          result = `${year}${monthStr}${dayStr}${hourStr}:${minuteStr}:${secondStr}`
          break
      }
      return result
    }
  }

}

export const dateFormat = (date) => new DateFormat(date)

export const getMonthDays = (date) => {
  let days
  let year = date.split('-')[0]
  let month = date.split('-')[1]
  if (month === '02') {
    days = year % 4 == 0 ? 29 : 28;
  } else if (month == '01' || month == '03' || month == '05' || month == '07' || month == '08' || month == '10' || month == '12') {
    days = 31
  } else {
    days = 30
  }
  return days
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值