好用的時候插件

由於歷久的項目積聚,本身寫了一些盤算時候的插件,相比較momoent,我以為比它好用

const ONE_DAY = 86400000
const ensureLength = function (num) {
  // 補全時候YYY-MM-dd HH:MM:ss
  let nlen = num.toString().length
  num = nlen < 2 ? '0' + num : num
  return num
}
const isUndefined = function (s) {
  return s === undefined || s === null || s === ''
}
const _DateUtils = {
  parse (dateStr) {
    if (dateStr instanceof Date) {
      return dateStr
    } else if (/^\d+$/.test(dateStr)) {
      return new Date(parseInt(dateStr, 10))
    } else if (dateStr) {
      let dateString = dateStr.replace(/[-:]/g, ',').replace(/\s+/g, ',')
      let dateArr = dateString.split(',')
      return new Date(dateArr[0], dateArr[1] - 1, dateArr[2], dateArr[3] || 0, dateArr[4] || 0, dateArr[5] || 0)
    } else if (!dateStr) {
      return dateStr
    }
  },
  toDateString (date, tpl) {
    let locals
    if (isUndefined(date)) {
      date = new Date()
    }
    if (isUndefined(tpl)) {
      tpl = 'yyyy-MM-dd'
    }
    date = this.parse(date)
    locals = {
      yyyy: date.getFullYear(),
      YYYY: date.getFullYear(),
      MM: ensureLength(date.getMonth() + 1),
      dd: ensureLength(date.getDate()),
      HH: ensureLength(date.getHours()),
      mm: ensureLength(date.getMinutes()),
      ss: ensureLength(date.getSeconds())

    }
    return tpl.replace(/YYYY|yyyy|MM|dd|HH|mm|ss/g, function ($1) {
      return locals[$1]
    })
  },
  toTimeString (date, tpl) {
    let locals
    if (isUndefined(date)) {
      date = new Date()
    }
    if (isUndefined(tpl)) {
      tpl = 'HH:mm:ss'
    }
    locals = {
      HH: ensureLength(date.getHours()),
      mm: ensureLength(date.getMinutes()),
      ss: ensureLength(date.getSeconds())
    }
    return tpl.replace(/HH|mm|ss/g, function ($1) {
      return locals[$1]
    })
  },
  toDateTimeString (date) {
    date = date || new Date()
    date = this.parse(date)
    return this.toDateString(date) + ' ' + this.toTimeString(date)
  },
  getFirstDate (date) {
    date = date || new Date()
    date = this.parse(date)
    let fitsrDate = new Date(date.getFullYear(), date.getMonth(), 1)
    return this.toDateString(fitsrDate)
  },
  getLastDate (date) {
    date = this.parse(date)
    let nextMonthFirstDate = (new Date(date.getFullYear(), date.getMonth() + 1, 1)).getTime()
    let lastDate = new Date(nextMonthFirstDate - ONE_DAY)
    return this.toDateString(lastDate)
  },
  toMonthString (date) {
    let month
    if (isUndefined(date)) {
      date = new Date()
    }
    date = this.parse(date)
    month = ensureLength(date.getMonth() + 1)
    return (date.getFullYear()) + '-' + month
  },
  getPreMonth (date) {
    let month, year
    if (isUndefined(date)) {
      date = new Date()
    }
    date = this.parse(date)
    year = date.getFullYear()
    month = ensureLength(date.getMonth())
    year = month === '00' ? year - 1 : year
    month = month === '00' ? '12' : month
    return year + '-' + month
  },
  getPreMonthDay (today) {
    if (isUndefined(today)) {
      today = new Date()
    }
    return this.toDateString(today.setMonth(today.getMonth() - 1))
  },
  ago (cnt) {
    let day = new Date().getDate()
    return new Date().setDate(day - cnt)
  },
  getCurrentWeek () {
    let now = new Date()
    let currentDay = now.getDay()
    let start = this.ago(currentDay - 1)
    let end = now
    return [start, end]
  },
  getRecentMonths (cnt) {
    /*
      params{
        cnt : '月',往後推幾個月
      }
    */
    if (isUndefined(cnt)) {
      cnt = 0
    }
    cnt = parseInt(cnt)
    let now = new Date()
    let currentMonth = now.getMonth()
    let year = now.getFullYear()
    let start
    let agoMonth = currentMonth - cnt > 0 ? currentMonth - cnt : currentMonth - cnt + 12
    let agoYear = currentMonth - cnt > 0 ? year : year - 1
    start = new Date(agoYear, agoMonth, 1)
    let end = new Date()
    return [start, end]
  },
  getCurrentQ () {
    let now = new Date()
    let currentMonth = now.getMonth()
    let start
    if (currentMonth < 3) {
      start = now.setMonth(0)
    } else if (currentMonth > 3 && currentMonth < 6) {
      start = now.setMonth(3)
    } else if (currentMonth > 2 && currentMonth < 6) {
      start = now.setMonth(6)
    } else if (currentMonth > 8 && currentMonth < 11) {
      start = now.setMonth(9)
    }
    start = new Date(start).setDate(1)
    let end = new Date()
    return [start, end]
  }
}
/*
params:{
  parse(格式化日期),
  toDateString(把時候戳只取日期部份,準確到天),
  toTimeString(把時候戳只取時候部份,準確到秒),
  toDateTimeString(把時候戳轉化為字符串情勢,準確到秒),
  getFirstDate(取月份第一天,準確到天),
  getLastDate(取月份末了一天,準確到天),
  toMonthString(月份轉化為字符串),
  getPreMonth(取上月份,準確到月),
  getPreMonthDay(取上一月,準確到天),
  ago(前第n天,準確到天),
  getRecentMonths(近來幾個月,從1號開始)
  getCurrentQ(近來一個季度,從1號開始)
}
*/
export default _DateUtils
    原文作者:一隻魚
    原文地址: https://segmentfault.com/a/1190000014810990
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞