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);
     }

  • 相关阅读:
    git 常用操作命令行
    Mysql 命令行...
    bootstrap
    10.11 android输入系统_补充知识_activity_window_decor_view关系
    10.10 android输入系统_APP获得并处理输入事件流程
    10.9 android输入系统_APP跟输入系统建立联系和Dispatcher线程_分发dispatch
    10.8 android输入系统_实战_使用GlobalKey一键启动程序
    10.7 android输入系统_Dispatcher线程情景分析_Reader线程传递事件和dispatch前处理
    10.6 android输入系统_Dispatcher线程_总体框架
    10.5 android输入系统_Reader线程_使用EventHub读取事件和核心类及配置文件_实验_分析
  • 原文地址:https://www.cnblogs.com/amylis_chen/p/3126917.html
Copyright © 2011-2022 走看看