js取今天日期的上一个月的数据

news/2024/7/10 23:59:14 标签: js, javascript, es6, jquery

前言

今天要做一个功能,但是数据只给到上一月,所以测试的时候需要指定用上一月的今天日期去取数据。
也就是说今天是07-10,我需要以6-10为基准去查前一天,前七天,前一月的数据
上网查了查,没有现成的,只能自己手敲T_T

代码如下

如有疏误,欢迎指出~
测试用例都写好了。。

2020-07-10更新

javascript"><!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<script>
  function MockTime(){
    //取当前日期,需要注意,月份索引从0开始,日期从1开始
    now =new Date()
    now.setMonth(2)
    now.setDate(31)
    console.log(now)  //3月31日

    let year=now.getFullYear().toString()
    let arr=[5,7,9,12]
    //如果取到月份数是[5,7,9,12],他们的上一个月是没有31号的,设置为30替代
    if (now.getDate()===31 && arr.includes(now.getMonth()+1)){
      now.setDate(30)
    }
    //判断闰年,如果不是闰年则2月只有28天,超过设置为28,为闰年则设置为29
    if((now.getFullYear()%4!==0)||(now.getFullYear()%100===0&&now.getFullYear()%400!==0)){
      //在不是闰年的情况下,若月份为3,则超过28都设为28
      if (now.getDate()>28 && now.getMonth()===2){
        now.setDate(28)
      }
    }else {
      //在是闰年的情况下,若月份为3,则超过29都设为29
      if (now.getDate()>29 && now.getMonth()===2){
        now.setDate(29)
      }
    }
    //月份补0,方便后面传递参数到数据库取数据
    // 此处now.getMonth(),若月份为3,得到2,不加1得到的就是我们想要的结果了
    let month=now.getMonth()<10? "0"+now.getMonth() : now.getMonth()
    //注意,如果当前为一月份,则需使年份-1,并且此时now.getMonth()为0,故需+12
    // 此时month为12,不需要补0
    if ((now.getMonth()+1) ===1 ){
      year=(now.getFullYear()-1).toString()
      month=(now.getMonth()+12).toString()
    }
    //日期补0,方便后面传递参数到数据库取数据
    let day = now.getDate()<10 ? "0"+now.getDate() : now.getDate();
    let this_time=year+'-'+month+'-'+day
    //返回得到的上一个月的日期
    return this_time
  }
  //得到模拟数据:当前日期前一个月的日期
let mock_time=  MockTime()
  console.log(mock_time)  // 2020-02-29
  let pre_day_time=PreDayTime(mock_time)
  console.log(pre_day_time)  //Object { startTime: "2020-02-28", endTime: "2020-02-28" }
  let pre_week_time=PreWeekTime(mock_time)
  console.log(pre_week_time)   //  Object { startTime: "2020-02-23", endTime: "2020-02-29" }
  let pre_month_time=PreMonthTime(mock_time)
  console.log(pre_month_time) //  Object { startTime: "2020-01-01", endTime: "2020-01-31" }

//传入当前时间,返回前一天的数据
  function PreDayTime(mydate) {
    let dateTime=getStringToDate(mydate)
    dateTime=dateTime.setDate(dateTime.getDate()-1);
    dateTime=new Date(dateTime);
    let start_time=getDateToString(dateTime)
    let this_time={
      startTime:start_time,
      endTime:start_time
    }
    return this_time
  }

  //传入当前时间,返回七天前日期
  function PreWeekTime(mydate){
    let dateTime=getStringToDate(mydate)
    let end_time=mock_time
    dateTime=dateTime.setDate(dateTime.getDate()-6);
    dateTime=new Date(dateTime);
    let start_time=getDateToString(dateTime)
    let this_time={
      startTime:start_time,
      endTime:end_time
    }

    return this_time
  }

  //传入当前时间,返回前一月日期
  function PreMonthTime(mydate) {
    let dateTime=getStringToDate(mydate)
    dateTime=dateTime.setMonth(dateTime.getMonth()-1)
    dateTime=new Date(dateTime);
    dateTime.setDate(1)
    let start_time=getDateToString(dateTime)
    dateTime.setDate(31)
    let end_time=getDateToString(dateTime)
    let this_time={
      startTime:start_time,
      endTime:end_time
    }
    return this_time
  }
  // 字符串转时间
  function getStringToDate(stringData) {
    //分割保存到数组
    let arr=stringData.split('-');
    //初始化时间,这里arr[1]要-1,因为索引0-11代表1-12月,如这里是06,其实代表7月,转成时间要减1
    let back_time=new Date(parseInt(arr[0]),parseInt(arr[1])-1,parseInt(arr[2]));
    return back_time
  }
  // 时间转字符串
  function getDateToString(date_data){
    let year=date_data.getFullYear()
    //月份补0,方便后面传递参数到数据库取数据
    //此处+1,索引的原因,转成字符串要加一
    let month=(date_data.getMonth()+1)<10? "0"+(date_data.getMonth()+1) : (date_data.getMonth()+1)
    //日期补0,方便后面传递参数到数据库取数据
    let date = date_data.getDate()<10 ? "0"+date_data.getDate() : date_data.getDate();
    let this_time=year+'-'+month+'-'+date
    return this_time
  }

</script>
</body>
</html>

结果:

javascript">Date Tue Mar 31 2020 10:37:18 GMT+0800 (中国标准时间)
testtime.html:14:13
2020-02-29 testtime.html:51:11
Object { startTime: "2020-02-28", endTime: "2020-02-28" }
testtime.html:53:11
Object { startTime: "2020-02-23", endTime: "2020-02-29" }
testtime.html:55:11
Object { startTime: "2020-01-01", endTime: "2020-01-31" }
testtime.html:57:11

ok,现在就可以指定日期取前一天,前七天,前一月的数据了!


http://www.niftyadmin.cn/n/1799076.html

相关文章

centos8下使用scp

Linux scp 命令用于 Linux 之间复制文件和目录。 scp 是 secure copy 的缩写, scp 是 linux 系统下基于 ssh 登陆进行安全的远程文件拷贝命令。 scp 是加密的&#xff0c;rcp 是不加密的&#xff0c;scp 是 rcp 的加强版。 命令&#xff1a; -r&#xff1a; 递归复制整个目…

在Win7中用ftp的方法

我的电脑是Win7 32位操作系统&#xff0c;之前一直没有成功使用过ftp 之初以为是ftp客户端的问题&#xff0c;换了几个软件都不行&#xff0c;然后换我的一台vps服务器&#xff08;win2003&#xff09;连接同一个ftp服务器可以正常连接使用。 那么就可以推断是win7操作系统的问…

内网centos7服务器离线安装python环境

前言 项目要部署到内网&#xff0c;所以需要离线安装python环境。 开始 系统是刚安装的centos7&#xff0c;系统信息 Linux 3.10.0-1062.el7.x86_64 #1 SMP Wed Aug 7 18:08:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linuxpython3.6.8安装包下载地址&#xff1a;Python-3.6.…

jqgrid 的编辑信息提示

在编辑时&#xff0c;无外乎两种结果&#xff1a;成功和失败。在form edit的弹出编辑窗体中隐藏了两个单元&#xff08;td&#xff09;&#xff0c;一个的ID是FormError&#xff0c;另一个没有id&#xff0c;有class叫做topinfo。就是这两个家伙可以分别来显示错误信息和操作成…

在内网Centos服务器部署python项目环境

内网&#xff1a;机器A&#xff08;Centos7&#xff09; 外网&#xff1a;机器B&#xff08;虚拟机&#xff0c;用的是跟机器A同一个iso文件&#xff09; 前置&#xff1a;已经安装好python3 首先&#xff0c;我们需要在机器A和B中建立虚拟环境 python -m venv aa-env 然后在机…

【知乎】一句话答案收录集,一句足矣

Q:摄影的意义是什么&#xff1f; We dont make a photograph just with a camera, we bring to the act of photography all the books we have read, the movies we have seen, the music we have heard, the people we have loved. 我们不只是用相机拍照, 我们带到摄影中去的…

ModuleNotFoundError: No module named ‘sqlite‘的解决方案

之前在内网centos服务器已经离线安装python3&#xff0c;建立虚拟环境并安装需要用到的库&#xff0c;安装好后将项目copy过来&#xff0c;运行时报错&#xff1a; ModuleNotFoundError: No module named _sqlite3在centos7环境中常规安装python3的话无法支持sqlite3,所以在安…

内网环境安装python所需的模块

内网电脑A 外网电脑B 电脑B&#xff1a; pip下载第三方包 pip download 你的包名 -d "下载的路径(windows下双引号来表示文件夹)" 如&#xff1a; pip download cryptography -d /file 通过scp传输到电脑 pip离线安装: pip install <包名>