zoukankan      html  css  js  c++  java
  • js 日期格式转换

    中国标准时间转换为特定格式时间(以2020-006-06为例)

        initDate(date){
          
          return  date.getFullYear() + "-" + (date.getMonth()> 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1)) + "-" +(date.getDate()> 9 ? (date.getDate()) : "0" + (date.getDate()));
        },
    

    本周、上周的开始日期和结束日期

        getTime(n) {
          let now = new Date();
            let year = now.getFullYear();
            let month = now.getMonth() + 1;
            let day = now.getDay(); //返回星期几的某一天;
            n = day == 0 ? n + 6 : n + (day - 1)
            now.setDate(now.getDate() - n);
            let date = now.getDate();
            let s = year + "-" + (month < 10 ? ('0' + month) : month) + "-" + (date < 10 ? ('0' + date) : date);
            return s;
        },
        /***参数都是以周一为基准的***/
        //上周的开始时间
        console.log(this.getTime(7));
        //上周的结束时间
        console.log(this.getTime(1));
        //本周的开始时间
        console.log(this.getTime(0));
        //本周的结束时间
        console.log(this.getTime(-6));
        //下周的开始时间
        console.log(this.getTime(-7))
        //下周的结束时间
        console.log(this.getTime(-13))
    

    上月开始时间和结束时间

     lastMonth(){
         let nowday = new Date();
         let year = nowday.getFullYear();
         let month = nowday.getMonth();
         if(month==0){
             month=12;
             year=year-1;
         }
         if (month < 10) {
              month = "0" + month;
         }
         let firstDayOfPreMonth = year + "-" + month + "-" + "01";
         let lastDay = new Date(year, month, 0);
         let lastDayOfPreMonth = year + "-" + month + "-" + lastDay.getDate();
         firstDayOfPreMonth = firstDayOfPreMonth.toString();
         lastDayOfPreMonth = lastDayOfPreMonth.toString();
         let value4 = [];
         value4.push(firstDayOfPreMonth); //开始时间
         value4.push(lastDayOfPreMonth); //结束时间
         return value4
      },
     // 输出结果为 ["2020-05-01", "2020-05-31"]
    

    本月开始日期、结束日期

          CurrentMonthLast(){
            let date=new Date();
            let currentMonth=date.getMonth();
            let nextMonth=++currentMonth;
            let nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
            let oneDay=1000*60*60*24;
            let start = this.initDate(new Date(new Date().setDate(1)))
            let end = this.initDate(new Date(nextMonthFirstDay-oneDay))
            return  [start,end] 
          },
          //输出结果 ["2020-06-01", "2020-06-30"]
    
  • 相关阅读:
    向量空间模型 词袋模型
    python 小点
    python list的append()和extend()方法
    numpy 数组运算
    内置函数和numpy中的min(),max()函数
    python index()函数
    Python支持的正则表达式元字符和语法
    Python多线程
    查看linux机器配置&内核版本
    Shell获取文件的文件名和扩展名的例子
  • 原文地址:https://www.cnblogs.com/FormerWhite/p/15016058.html
Copyright © 2011-2022 走看看