zoukankan      html  css  js  c++  java
  • js日期计算及快速获取周、月、季度起止日

    var now = new Date();                                                //当前日期
     var nowDayOfWeek = (now.getDay() == 0) ? 7 : now.getDay() - 1;       //今天是本周的第几天。周一=0,周日=6
     var nowDay = now.getDate();                                          //当前日
     var nowMonth = now.getMonth();                                       //当前月值(1月=0,12月=11)
     var nowMonReal = now.getMonth() + 1;                                 //当前月实际数字
     var nowYear = now.getFullYear();                                     //当前年
     
     //日期+天
     function AddDays(d, n) {
         var t = new Date(d);//复制并操作新对象,避免改动原对象
         t.setDate(t.getDate() + n);
         return t;
     }
     
     //日期+月。日对日,若目标月份不存在该日期,则置为最后一日
     function AddMonths(d, n) {
         var t = new Date(d);
         t.setMonth(t.getMonth() + n);
         if (t.getDate() != d.getDate()) { t.setDate(0); }
         return t;
     }
     
     //日期+年。月对月日对日,若目标年月不存在该日期,则置为最后一日
     function AddYears(d, n) {
         var t = new Date(d);
         t.setFullYear(t.getFullYear() + n);
         if (t.getDate() != d.getDate()) { t.setDate(0); }
         return t;
     }
     
     //获得本季度的开始月份
     function getQuarterStartMonth() {
         if (nowMonth <= 2) { return 0; }
         else if (nowMonth <= 5) { return 3; }
         else if (nowMonth <= 8) { return 6; }
         else { return 9; }
     }
     
     //周一
     function getWeekStartDate() {
         return AddDays(now, -nowDayOfWeek);
     }
     
     //周日。本周一+6天
     function getWeekEndDate() {
         return AddDays(getWeekStartDate(), 6);
     }
     
     //月初
     function getMonthStartDate() {
         return new Date(nowYear, nowMonth, 1);
     }
     
     //月末。下月初-1天
     function getMonthEndDate() {
         return AddDays(AddMonths(getMonthStartDate(), 1), -1);
     }
     
     //季度初
     function getQuarterStartDate() {
         return new Date(nowYear, getQuarterStartMonth(), 1);
     }
     
     //季度末。下季初-1天
     function getQuarterEndDate() {
         return AddDays(AddMonths(getQuarterStartDate(), 3), -1);
     }
  • 相关阅读:
    【转】sql server编写通用脚本自动检查两个不同服务器的新旧数据库的表结构差异
    Pytest 2
    【转】python通过SMTP协议发送邮件失败,报错505或535
    【转】环境搭建之allure的安装配置,及简单使用
    Pytest 1
    替换姓名为隐式
    docker 用户组权限
    安装go环境
    Win10配置WSL2安装Ubuntu,并支持Nvidia CUDA 环境
    miniconda源配置
  • 原文地址:https://www.cnblogs.com/dekevin/p/4550850.html
Copyright © 2011-2022 走看看