日期函数(最近一周/一个月/半年/一年)

这篇博客介绍了JavaScript中用于获取最近一周、一个月、三个月、半年和一年时间范围的函数。通过设置Date对象的时间并计算时间差,可以方便地获取这些时间段的开始和结束日期。这些函数对于数据分析和日志记录等场景非常有用。
摘要由CSDN通过智能技术生成

日期函数(最近一周/一个月/半年/一年)

// getTime 返回 1970 年 1 月 1 日至今的毫秒数。
// setTime(毫秒)  方法以毫秒设置 Date 对象。
//今天
function today(){
    const newDay = new Date();
    return newDay.getFullYear()+"-" + (newDay.getMonth()+1) + "-" + newDay.getDate()
  }
  //最近一周
  function lastWeek(){
    const end = new Date();
    const start = new Date();
    start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
    return [start.getFullYear()+"-" + (start.getMonth()+1) + "-" + start.getDate(),end.getFullYear()+"-" + (end.getMonth()+1) + "-" + end.getDate()]
  }
  //最近一个月
  function lastMonth(){
    const end = new Date();
    const start = new Date();
    start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
    return [start.getFullYear()+"-" + (start.getMonth()+1) + "-" + start.getDate(),end.getFullYear()+"-" + (end.getMonth()+1) + "-" + end.getDate()]
  }
  //最近三个月
  function lastThreeMonths(){
    const end = new Date();
    const start = new Date();
    start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
    return [start.getFullYear()+"-" + (start.getMonth()+1) + "-" + start.getDate(),end.getFullYear()+"-" + (end.getMonth()+1) + "-" + end.getDate()]
  }
  //最近半年
  function lastSixMonths(){
    const end = new Date();
    const start = new Date();
    start.setTime(start.getTime() - 3600 * 1000 * 24 * 182);
    return [start.getFullYear()+"-" + (start.getMonth()+1) + "-" + start.getDate(),end.getFullYear()+"-" + (end.getMonth()+1) + "-" + end.getDate()]
  }
  //最近一年
  function lastYear(){
    const end = new Date();
    const start = new Date();
    start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
    return [start.getFullYear()+"-" + (start.getMonth()+1) + "-" + start.getDate(),end.getFullYear()+"-" + (end.getMonth()+1) + "-" + end.getDate()]
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值