首页 > 其他分享 >mytime.js 时间的常用计算,配合使用

mytime.js 时间的常用计算,配合使用

时间:2022-10-27 09:56:51浏览次数:29  
标签:function 常用 return mytime let Date new js getMonth

      //获得本周的开始日期
      function getWeekStartDate() {
        let weekStartDate = new Date(
          new Date().getFullYear(),
          new Date().getMonth(),
          new Date().getDate() - new Date().getDay() + 1
        );
        return this.formatDate(weekStartDate);
      }
      //获得本周的结束日期
      function getWeekEndDate() {
        let weekEndDate = new Date(
          new Date().getFullYear(),
          new Date().getMonth(),
          new Date().getDate() + (7 - new Date().getDay())
        );
        return this.formatDate(weekEndDate);
      }
      //获得本年的开始日期
      function getYearStartDate() {
        //获得当前年份4位年
        let currentYear = new Date().getFullYear();
        //本年第一天
        let currentYearFirstDate = new Date(currentYear, 0, 1);
        return this.formatDate(currentYearFirstDate);
      }
      //获得本年的结束日期
      function getYearEndDate() {
        //获得当前年份4位年
        let currentYear = new Date().getFullYear();
        //本年最后
        let currentYearLastDate = new Date(currentYear, 11, 31);
        return this.formatDate(currentYearLastDate);
      }
      //获得本季度的开始日期
      function getQuarterStartDate() {
        let quarterStartDate = new Date(
          new Date().getFullYear(),
          this.getQuarterStartMonth(),
          1
        );
        return this.formatDate(quarterStartDate);
      }
      //获的本季度的结束日期
      function getQuarterEndDate() {
        let quarterEndMonth = this.getQuarterStartMonth() + 2;
        let quarterStartDate = new Date(
          new Date().getFullYear(),
          quarterEndMonth,
          this.getMonthDays(quarterEndMonth)
        );
        return this.formatDate(quarterStartDate);
      }
      //获得本季度的开始月份
      function getQuarterStartMonth() {
        let quarterStartMonth = 0;
        if (new Date().getMonth() < 3) {
          quarterStartMonth = 0;
        }
        if (2 < new Date().getMonth() && new Date().getMonth() < 6) {
          quarterStartMonth = 3;
        }
        if (5 < new Date().getMonth() && new Date().getMonth() < 9) {
          quarterStartMonth = 6;
        }
        if (new Date().getMonth() > 8) {
          quarterStartMonth = 9;
        }
        return quarterStartMonth;
      }
      //获得某月的天数 new Date().getMonth()不加1就是当月天数
      function getMonthDays(myMonth) {
        let monthStartDate = new Date(new Date().getFullYear(), myMonth, 1);
        let monthEndDate = new Date(new Date().getFullYear(), myMonth + 1, 1);
        let days =
          (Number(monthEndDate) - Number(monthStartDate)) /
          (1000 * 60 * 60 * 24);
        return days;
      }
      //获得本月的开始日期
      function getMonthStartDate() {
        let monthStartDate = new Date(
          new Date().getFullYear(),
          new Date().getMonth(),
          1
        );
        return this.formatDate(monthStartDate);
      }
      //获得本月的结束日期
      function getMonthEndDate() {
        let monthEndDate = new Date(
          new Date().getFullYear(),
          new Date().getMonth(),
          this.getMonthDays(new Date().getMonth())
        );
        return this.formatDate(monthEndDate);
      }
      //获得上月的开始日期
      function getLastMonthStartDate() {
        let nowYear = new Date().getFullYear();
        let lastMonthDate = new Date(); //上月日期
        lastMonthDate.setDate(1);
        lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
        let lastMonth = lastMonthDate.getMonth();
        let lastMonthStartDate = new Date(nowYear, lastMonth, 1);
        return formatDate(lastMonthStartDate);
      }
      //获得上月的结束日期
      function getLastMonthEndDate() {
        let nowYear = new Date().getFullYear();
        let lastMonthDate = new Date(); //上月日期
        lastMonthDate.setDate(1);
        lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
        let lastMonth = lastMonthDate.getMonth();

        let lastMonthEndDate = new Date(
          nowYear,
          lastMonth,
          getMonthDays(lastMonth)
        );
        return formatDate(lastMonthEndDate);
      }
      // 格式化时间
      function formatDate(value) {
        let date = new Date(value);
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        m = m < 10 ? "0" + m : m;
        let d = date.getDate();
        d = d < 10 ? "0" + d : d;
        let time = y + "-" + m + "-" + d;
        return time;
      }

 

标签:function,常用,return,mytime,let,Date,new,js,getMonth
From: https://www.cnblogs.com/myfirstboke/p/16831086.html

相关文章

  • js 导出excle文件(多页)
     --前提:页面生成相应的表格数据  例如:<tableid="a"> <tr> <th></th> </tr> <tr> <td></td> </tr> </t......
  • 一些常用SQL
    Dataworks(Maxcompute)获取昨天日期selectreplace(date_add(getdate(),-1),'-','')Hologres(pgsql)pg_typeof()类似python中的type()函数,用于获取数据类型select......
  • nginx 常用命令
    命令查看nginx版本号命令/usr/local/nginx-v查看nginx安装的参数/安装的模块本质就是看编译安装中./configure步骤添加的参数命令/usr/local/nginx-V发......
  • 删除 设置 获取 cookies 的第三方包 js-cookies
    //第三方包js-cookies是一个操作cookies的包importCookiesfrom'js-cookie'//Cookies.set(key,value)存值//Cookies.get(key)取值//Cookies.remove(key)......
  • ReactJS单页面应用之项目搭建
    初衷因接手的项目前端采用reactjs+antd,为把控项目中的各个细节,所以想做一些整理,以免后期遗忘。创建及启动项目#全局安装create-react-app#如果曾经安装过,可先移除:n......
  • js 预解析
    预解析是js中一个很重要的知识,为了防止忘记和便于后续查找,本博客记录了预解析的重要概念和一些例子。一、什么是预解析。js引擎运行js会分成两个步骤,一......
  • b站 bilibili 哔哩哔哩 js小脚本 油猴 查找搜索某年的视频 自动翻页
    varccount=0;varnumnum=5constsleep=(delay)=>newPromise((resolve)=>setTimeout(resolve,delay))while(numnum--){  document.querySelect......
  • js 的页面切换
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content......
  • TypeError: Object of type 'ndarray' is not JSON serializabl;TypeError: Object of
    1.python使用​​json.jsonify​​​将结果转为json格式时,爆出如上​​TypeError:Objectoftype'ndarray'isnotJSONserializable​​​错误。(flask)代码如下:@app......
  • Python pandas DataFrame 行列使用常用操作
    Pandas是基于NumPy的一种工具,该工具是为了解决数据分析任务而创建的。Pandas纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。Pandas提供了大量......