zoukankan      html  css  js  c++  java
  • JS 获取当天所在月的第一天的日期,最后一天日期,所在周的每天的日期,时间,所在每月日期,时间的计算

    /**
     * 获取当天所在月的最后一天日期,和下一个月的第一天日期
     * 调用getMonthDay(d), d默认值为01,d为要设置成的day;
     */
    const getMonthMaxDay = () => {
      var curDate = new Date();
      var curMonth = curDate.getMonth();
      /*  生成实际的月份: 由于curMonth会比实际月份小1, 故需加1 */
      curDate.setMonth(curMonth + 1);
      /* 将日期设置为0, 返回当月最后一天日期, 将日期设置为1,返回下一个月第一天日期*/
      curDate.setDate(0);
      /* 返回当月的天数 */
      return curDate.getDate();
    }
    
    // 获取当前月中第某一天的日期
    export const getMonthDay = (d='01') => {
      const t = new Date();
      return `${t.getFullYear()}-${setT(t.getMonth() + 1)}-${d}`;
    }
    const setT = (e) => {
      return `${e}`.length > 1 ? `${e}` : `0${e}`;
    }
    getMonthDay() // return 2017-05-01
    getMonthDay(getMonthMaxDay); //return 2017-05-31
    
    
    
    /**
     * 获取当天所在周的周一和周日的日期
     * 返回 YYYY-MM-DD 格式时间
     */
    const setToday = function (t=(new Date())) {
      return `${t.getFullYear()}-${setT(t.getMonth() + 1)}-${setT(t.getDate())}`;
    }
    export const getWeekDay = () => {
      const date1 = new Date(),
            date2 = new Date();
      return {
        weekStart: setToday(new Date(date1.setDate(date1.getDate() - date1.getDay() + 1))), // Monday
        weekEnd: setToday( new Date(date2.setDate(date2.getDate() + (7 - date2.getDay())))), // Sunday
      };
    }
    getWeekDay() //return {weekStart: 2017-05-22, weekEnd: 2017-05-28}
    
    /**
     * 对时间进行加减运算
     * calcTime(t, d) t参数为要进行计算的日期,d为时间差
     * 返回 YYYY-MM-DD HH:mm:ss 格式时间
     */
    const setFullTime = function(t=(new Date())) {
      return `${t.getFullYear()}-${setT(t.getMonth() + 1)}-${setT(t.getDate())} ${setT(t.getHours())}:${setT(t.getMinutes())}:${setT(t.getSeconds())}`;
    }
    export const calcTime = (t, d) => {
      var date1 = new Date((t).replace(/-/g, '/')),
          date2 = (date1/1000 + d)*1000,
          date3 = new Date(date2);
      return setFullTime(date3);
    };
    const d1 = '2017-05-26 18:08:45';
    cosnt d = -30;
    calcTime(d1, d) // return 2017-05-26 18:08:15
     1 const setT = (e) => {
     2           return `${e}`.length > 1 ? `${e}` : `0${e}`;
     3         }
     4         const setToday = function(t=(new Date())) {
     5           return `${t.getFullYear()}-${setT(t.getMonth() + 1)}-${setT(t.getDate())}`;
     6         }
     7         const date1 = new Date()
     8         this.firstDayCurrent =  setToday(new Date(date1.setDate(date1.getDate() - (date1.getDate() -1))))
     9         console.log('第一天是' + this.firstDayCurrent);
    10         //判断是否是当前月,如果不是循环所选月份
    11 
    12
    13           var changeDate = new Date(this.firstDayCurrent)
    14           var changeYear = changeDate.getFullYear();
    15           var changeMonth = changeDate.getMonth() + 1;
    16           var changeNow = new Date(changeYear,changeMonth,0);
    17           this.changeCount = changeNow.getDate();  //获取当月天数
    18           
    19           console.log(this.changeCount)
    20           for(var i=0;i< this.changeCount; i ++){
    21 
    22             //获取当月的每一天
    23             const getChangeWeekDay = () => {
    24               // date2 = new Date();
    25               // console.log(date1.getDay())  //获取星期几
    26               return  setToday(new Date(changeDate.setDate(changeDate.getDate() - (changeDate.getDate() -1) + i ))) // 循环获取每一天
    27               //
    28             }
    29        getChangeWeekDay()
    30       }
            
  • 相关阅读:
    1019.安全技能树
    1020.Burp Suite扩展之Java-Deserialization-Scanner
    1018.渗透利器
    1016.XXE漏洞攻防学习
    1017.前端黑在线工具
    1015.WebGoat SQL注入之 Order by注入解题思路
    1014.WebGoat SQL盲注 解题思路
    2019春节防坑指南之抢票陷阱(文末有彩蛋)
    【年度大戏】勒索”嘿客“无间道之战
    470余万条疑似12306用户数据遭贩卖 嫌疑人被刑拘
  • 原文地址:https://www.cnblogs.com/yun1108/p/9778411.html
Copyright © 2011-2022 走看看