js日期實用工具函數

獲取當前日期

// 獲取當前日期
function getNowDate () {
  let date = new Date()
  let Y = date.getFullYear()
  let M = date.getMonth() + 1
  let D = date.getDate()
  M = M < 10 ? `0${M}` : M
  D = D < 10 ? `0${D}` : D
  return `${Y}-${M}-${D}`
}

獲取當前日期前n個月

// 獲取當前日期前n個月的日期
function getPreMonthDay (date, monthNum) {
  let dateArr = date.split('-')
  let year = dateArr[0] // 獲取當前日期的年份
  let month = dateArr[1] // 獲取當前日期的月份
  let day = dateArr[2] // 獲取當前日期的日
  // let days = new Date(year, month, 0)
  // days = days.getDate() //獲取當前日期中月的天數
  let year2 = year
  let month2 = parseInt(month) - monthNum
  if (month2 <= 0) {
    year2 =
      parseInt(year2) -
      parseInt(month2 / 12 === 0 ? 1 : Math.abs(parseInt(month2 / 12)) + 1)
    month2 = 12 - (Math.abs(month2) % 12)
  }
  let day2 = day
  let days2 = new Date(year2, month2, 0)
  days2 = days2.getDate()
  if (day2 > days2) {
    day2 = days2
  }
  if (month2 < 10) {
    month2 = '0' + month2
  }
  let t2 = year2 + '-' + month2 + '-' + day2
  return t2
}

判斷是否爲合法日期

/**
 * @method 判斷是否爲合法日期
 * @param date 爲Date轉化後的值
 */
function isValidDate (date) {
  return date instanceof Date && !isNaN(date.getTime())
}

判斷一個時間是否位於兩時間之間

/**
 * @method 判斷一個時間是否位於兩時間之間
 * @param sDate1,sDate2爲日期,如:2019-01-01
 */
function dateBetweenJudget (startDate, endDate, date0) {
  let oStartDate = new Date(startDate)
  let oEndDate = new Date(endDate)
  let oDate0 = new Date(date0)
  if ((oEndDate.getTime() >= oDate0.getTime()) && (oStartDate.getTime() <= oDate0.getTime())) {
    return true
  }
  return false
}

比較兩個日期的大小

/* @method 比較兩個日期的大小
 * @param sDate1,sDate2爲日期,如:2019-01-01
 */
function dateJudget (sDate1, sDate2) {
  var oDate1 = new Date(sDate1)
  var oDate2 = new Date(sDate2)
  if (oDate1.getTime() > oDate2.getTime()) {
    return false
  }
  return true
}

獲取當前月份前n個月或者後n個月(返回日期列表)

/**
 * @method 獲取當前月份前n個月或者後n個月
 * @param range 範圍,before表示前n個月,after表示後n個月
 * @param date 特定的日期,如2019-01-01,若傳‘’,默認爲當前日期
 */
function getMonthRange (range, n, date = '') {
  var monthArr = []
  let data = (date === '') ? new Date() : new Date(date)
  if (range === 'before') {
    data.setMonth(data.getMonth() + 1, 1) // 獲取到當前月份,設置月份
  } else {
    data.setMonth(data.getMonth() - 1, 1) // 獲取到當前月份,設置月份
  }
  for (var i = 0; i < n; i++) {
    if (range === 'before') {
      data.setMonth(data.getMonth() - 1) // 每次循環一次 月份值減1
    } else {
      data.setMonth(data.getMonth() + 1) // 每次循環一次 月份值加1
    }
    let m = data.getMonth() + 1
    let m2 = m < 10 ? '0' + m : m
    let json = {
      yearAndMonth: data.getFullYear() + '-' + m2,
      month: m,
      year: data.getFullYear(),
    }
    monthArr.push(json)
  }
  return monthArr
}

獲取某個月有多少天

/**
 * @method 獲取某個月有多少天數
 * @param year 年
 * @param month 月(如:2)
 */
function getDaysInMonth (year, month) {
  return new Date(year, month, 0).getDate()
}

獲取當前日期的前一個月

/**
 * @method 獲取當前日期的前一個月
 */
function getPreMonthDate (times) {
  times = new Date(times)
  let Y = times.getFullYear()
  let M = times.getMonth() + 1
  const D = times.getDate()
  if (M === 1) {
    M = 12
    Y = Y - 1
  } else {
    M = M - 1
  }
  return `${Y}-${M}-${D}`
}

獲取當前日期的前兩個月

/**
 * @method 獲取當前日期的前兩個月
 */
function getPreMonthDate2 (times) {
  times = new Date(times)
  let Y = times.getFullYear()
  let M = times.getMonth() + 1
  const D = times.getDate()
  if (M === 2) {
    M = 12
    Y = Y - 1
  } else if (M === 1) {
    M = 11
    Y = Y - 1
  } else {
    M = M - 2
  }
  return `${Y}-${M}-${D}`
}

根據日期判斷今天周幾

/**
 * @method 根據日期判斷今天周幾
 * @param {*} date 日期格式爲字符串2018-01-01
 */
function getDateWeek (date) {
  const weekDay = ['週日', '週一', '週二', '週三', '週四', '週五', '週六']
  const myDate = new Date(Date.parse(date))
  return weekDay[myDate.getDay()]
}

判斷兩個日期之間的天數差值

/**
 * @method 判斷兩個日期之間的天數差值
 * @param {*} endDate 結束天數 日期格式爲字符串2018-01-01
 * @param {*} startDate 開始日期 日期格式爲字符串2018-01-01
 */
function DateDifference (endDate, startDate) {
  return parseInt((Math.abs(new Date(endDate) - new Date(startDate)) / 1000 / 60 / 60 / 24) + 1)
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章