js 获取今日/昨日/本周/上周/当月/上月开始和结束的时间戳

获取今日零点时间戳

/**
 * 获取今日零点时间戳
 * @returns {number}
 */
export function todayStartTimestamp() {
  const timestamp = Math.floor(new Date(new Date().setHours(0, 0, 0, 0)).getTime() / 1000)
  return timestamp
}

获取今日结束时间戳

/**
 * 获取今日结束时间戳
 * @returns {number}
 */
export function todayEndTimestamp() {
  const timestamp = Math.floor(new Date(new Date().setHours(23, 59, 59, 999)).getTime() / 1000)
  return timestamp
}

获取昨日开始、结束时间戳

/**
 * 获取昨日开始、结束时间戳
 * @param num
 * @returns {number[昨日开始时间戳, 昨日结束时间戳]}
 */
export function yesterdayTimestamp(num = 1) {
  const MillisecondsADay = 24 * 60 * 60 * num
  //  今日零点时间戳
  const timestamp = Math.floor(new Date(new Date().setHours(0, 0, 0, 0)).getTime() / 1000)
  // 昨日开始时间戳
  const yesterdayStartTime = timestamp - MillisecondsADay
  // 昨日结束时间戳
  const yesterdayEndTime = timestamp - 1
  return [yesterdayStartTime, yesterdayEndTime]
}

本周开始时间戳

/**
 * 本周开始时间戳
 * @returns {number}
 */
export function weekStartTimestamp() {
  //  一天的秒数
  const MillisecondsADay = 24 * 60 * 60
  //  今日零点时间戳
  const timestamp = Math.floor(new Date(new Date().setHours(0, 0, 0, 0)).getTime() / 1000)
  const weekDay = new Date().getDay() === 0 ? (7 - 1) : (new Date().getDay() - 1)
  const weekTimeStamp = timestamp - MillisecondsADay * weekDay
  return weekTimeStamp
}

上周开始、结束时间戳

/**
 * 上周开始、结束时间戳
 * @returns {number[上周开始时间戳, 上周结束时间戳]}
 */
export function lastWeekTimetamp() {
  //  一天的秒数
  const MillisecondsADay = 24 * 60 * 60
  //  今日零点时间戳
  const timestamp = Math.floor(new Date(new Date().setHours(0, 0, 0, 0)).getTime() / 1000)
  const weekDay = new Date().getDay() === 0 ? (7 - 1) : (new Date().getDay() - 1)
  //  本周开始时间戳
  const weekTimeStamp = timestamp - MillisecondsADay * weekDay
  //  上周开始时间戳
  const lastWeekStart = weekTimeStamp - MillisecondsADay * 7
  //  上周结束时间戳
  const lastWeekEnd = weekTimeStamp - 1
  return [lastWeekStart, lastWeekEnd]
}

当月开始时间戳

/**
 * 当月开始时间戳
 * @returns {number}
 */
export function monthStartTimestamp() {
  const date = new Date()
  date.setDate(1)
  date.setHours(0, 0, 0, 0)
  const timeStamp = date.getTime() / 1000
  return timeStamp
}

获取上月开始、结束时间戳

/**
 * 获取上月开始、结束时间戳
 * @returns {number[上月开始时间戳,上月结束时间戳]}
 */
export function lastMonthTimetamp() {
  //  一天的秒数
  const MillisecondsADay = 24 * 60 * 60

  const date = new Date()
  date.setDate(1)
  date.setHours(0, 0, 0, 0)
  //  当月开始时间戳
  const timeStamp = date.getTime() / 1000
  //  上个月的天数
  const days = lastMonthDats()
  //  上月开始时间戳
  const lastMonthStart = timeStamp - (MillisecondsADay * days)
  //  上月结束时间戳
  const lastMonthEnd = timeStamp - 1
  return [lastMonthStart, lastMonthEnd]
}
/**
 * 上月天数
 * @returns {number}
 */
function lastMonthDats() {
  const date = new Date()
  const year = date.getFullYear()
  //  上个月月份
  let month = (date.getMonth() + 1) - 1 //  0-11 表示 1月-12月
  //  0 表示12月
  month = month || 12
  //  30天的月份
  const arr30 = [4, 6, 9, 11]
  //  31天的月份
  const arr31 = [1, 3, 5, 7, 8, 10, 12]
  if (arr30.indexOf(month) !== -1) {
    //  上个月是 30 天
    return 30
  } else if (arr31.indexOf(month) !== -1) {
    //  上个月是 31 天
    return 31
  } else {
    //  2月
    if (isRunYear(year)) {
      return 29
    } else {
      return 28
    }
  }
}
/**
 * 是否为闰年
 * @param year
 * @returns {boolean}
 */
function isRunYear(year) {
  //  条件:能被4整除并且不能被100整除,或者被400整除的
  let flag = false
  if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
    flag = true
  }
  return flag
}
posted @ 2022-04-07 10:44  夏雨言  阅读(3087)  评论(1编辑  收藏  举报