js日期范围:本周/上周/本月/上月

用于日期范围选择器快捷项,忽略具体时间,包含今日/昨日/本周/上周/本月/上月。

/**
 * 今日
 * @returns {Date[]}
 */
function today () {
  const now = new Date()
  return [now, now]
}

/**
 * 昨日
 * @returns {Date[]}
 */
function yesterday () {
  const now = new Date()
  const yesterday = new Date()
  yesterday.setTime(now.getTime() - 24 * 60 * 60 * 1000)
  return [yesterday, yesterday]
}

/**
 * 本周
 * @returns {Date[]}
 */
function thisWeek () {
  const monday = new Date()
  const now = new Date()
  const todayOfWeek = now.getDay() || 7
  monday.setTime(now.getTime() - (todayOfWeek - 1) * 24 * 60 * 60 * 1000)
  return [monday, now]
}

/**
 * 上周
 * @returns {Date[]}
 */
function lastWeek () {
  const monday = new Date()
  const sunday = new Date()
  const now = new Date()
  const todayOfWeek = now.getDay() || 7
  sunday.setTime(now.getTime() - todayOfWeek * 24 * 60 * 60 * 1000)
  monday.setTime(sunday.getTime() - 6 * 24 * 60 * 60 * 1000)
  return [monday, sunday]
}

/**
 * 本月
 * @returns {Date[]}
 */
function thisMonth () {
  const firstDay = new Date()
  const now = new Date()
  firstDay.setDate(1)
  return [firstDay, now]
}

/**
 * 上月
 * @returns {Date[]}
 */
function lastMonth () {
  const lastDay = new Date()
  lastDay.setDate(0)
  const firstDay = new Date(lastDay)
  firstDay.setDate(1)
  return [firstDay, lastDay]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值