JavaScript 日期相关操作

记录一些常用的日期操作

/**
 * 使用 es6 的语法糖
 * 定义一个日期处理“类”
 * 使用 static 修饰供给外部调用的方法,不用实例化,直接通过 DateModel.xxx 即可
 */
class DateModel {
  /**
   * 定义一些后面要是用的变量
   */
  static DayType = 'day'
  static MinuteType = 'minute'
  static SecondType = 'second'
  static OneDaySeconds = 60 * 60 * 24 * 1000

   // 获取前十二个月时间
  static getLastTwelveMonth(date) {
    let _date = new Date(date)
    const month = _date.getMonth()
    _date.setMonth(month - 11)
    return DateModel.formateDate(_date)
  }
  
  /**
   * 获取某月第一天
   * @param date 需要获取第一天的月份日期
   */
  static getStartDateOfMonth(date = new Date()) {
    const _date = DateModel.formateDate(date)
    const dateArr = _date.split('-')
    return `${dateArr[0]}-${dateArr[1]}-01`
  }
  /**
   * 获取某月最后一天
   * @param date 需要获取最后一天的月份日期
   */
  static getEndDateOfMonth(date = new Date()) {
    const _date = new Date(date)
    const year = _date.getFullYear()
    const month = _date.getMonth() + 1
    return DateModel.formateDate(new Date(year, month, 0))
  }
  /**
   * 获取上月的第一天
   * @param date
   */
  static getStartDateOfLastMonth(date = new Date()) {
    const _date = new Date(date)
    const year = _date.getFullYear(_date)
    const month = _date.getMonth() - 1
    return DateModel.formateDate(new Date(year, month, 1))
  }

  /**
   * 获取上周的第一天
   * 1、获取上周的这个时间
   * 2、获取当前周的第一天即可
   */
  static getStartDateOfLastWeek(date = new Date()) {
    const _date = + new Date(date)
    const oneWeekSeconds = 7 * DateModel.OneDaySeconds
    return DateModel.getStartDateOfWeek(_date - oneWeekSeconds)
  }

  /**
   * 获取当前周的第一天
   */
  static getStartDateOfWeek(date = new Date()) {
    const _date = new Date(date)
    let day = _date.getDay()
    day = day === 0 ? 7 : day
    day = day - 1
    const dis = day * DateModel.OneDaySeconds
    return DateModel.formateDate(+_date - dis)
  }
  /**
   * 获取当前周的最后一天
   */
  static getEndDateOfWeek(date = new Date()) {
    const _date = new Date(date)
    let day = _date.getDay()
    day = day === 0 ? 7 : day
    const dis = (7 - day) * DateModel.OneDaySeconds
    return DateModel.formateDate(+_date + dis)
  }

  /**
   * 获取当前日期是星期几
   */
  static getWeekdayOfDate(date = new Date()) {
    const _date = new Date(date)
    let day = _date.getDay()
    day = day === 0 ? 7 : day
    return day
  }
  /**
   * 获取当前月份有多少天
   * 1、获取当前月份最后一天
   * 2、取最后一天的日期数
   */
  static getDateCountOfMonth(date = new Date()) {
    const lastDateOfMonth = DateModel.getEndDateOfMonth(date)
    return Number(lastDateOfMonth.split('-')[2])
  }

  /**
   * 格式化日期
   * @param date 时间戳/不是统一的日期格式
   * @param type day minute second 默认年月日、根据需要返回时分 时分秒
   */
  static formateDate(date = new Date(), type = DateModel.DayType) {
    const _date = new Date(date)
    const year = _date.getFullYear()

    let month = _date.getMonth() + 1
    month = DateModel.zeroFilling(month)

    let day = _date.getDate()
    day = DateModel.zeroFilling(day)

    let hour = _date.getHours()
    hour = DateModel.zeroFilling(hour)

    let minute = _date.getMinutes()
    minute = DateModel.zeroFilling(minute)

    let second = _date.getSeconds()
    second = DateModel.zeroFilling(second)

    const baseDate = `${year}-${month}-${day}`
    switch (type) {
      case DateModel.DayType:
        return baseDate;
      case DateModel.MinuteType:
        return `${baseDate} ${hour}:${minute}`;
      case DateModel.SecondType:
        return `${baseDate} ${hour}:${minute}:${second}`;
    }
  }

  /**
   * 月份、日期补零操作
   * 小于10 变成 '0x'
   */
  static zeroFilling(num) {
    return num < 10 ? `0${num}` : num
  }
}

你可能感兴趣的:(JavaScript 日期相关操作)