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
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM