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

    机缘巧合,这段接触了一下js,刚开始各种磕碰各种不顺手,一个日期计算都折腾我半天,积累了一些,赶紧码下:  

     1 var now = new Date();                                                //当前日期
     2 var nowDayOfWeek = (now.getDay() == 0) ? 6 : now.getDay() - 1;       //今天是本周的第几天。周一=0,周日=6
     3 var nowDay = now.getDate();                                          //当前日
     4 var nowMonth = now.getMonth();                                       //当前月值(1月=0,12月=11)
     5 var nowMonReal = now.getMonth() + 1;                                 //当前月实际数字
     6 var nowYear = now.getFullYear();                                     //当前年
     7 
     8 //日期+天
     9 function AddDays(d, n) {
    10     var t = new Date(d);//复制并操作新对象,避免改动原对象
    11     t.setDate(t.getDate() + n);
    12     return t;
    13 }
    14 
    15 //日期+月。日对日,若目标月份不存在该日期,则置为最后一日
    16 function AddMonths(d, n) {
    17     var t = new Date(d);
    18     t.setMonth(t.getMonth() + n);
    19     if (t.getDate() != d.getDate()) { t.setDate(0); }
    20     return t;
    21 }
    22 
    23 //日期+年。月对月日对日,若目标年月不存在该日期,则置为最后一日
    24 function AddYears(d, n) {
    25     var t = new Date(d);
    26     t.setFullYear(t.getFullYear() + n);
    27     if (t.getDate() != d.getDate()) { t.setDate(0); }
    28     return t;
    29 }
    30 
    31 //获得本季度的开始月份
    32 function getQuarterStartMonth() {
    33     if (nowMonth <= 2) { return 0; }
    34     else if (nowMonth <= 5) { return 3; }
    35     else if (nowMonth <= 8) { return 6; }
    36     else { return 9; }
    37 }
    38 
    39 //周一
    40 function getWeekStartDate() {
    41     return AddDays(now, -nowDayOfWeek);
    42 }
    43 
    44 //周日。本周一+6天
    45 function getWeekEndDate() {
    46     return AddDays(getWeekStartDate(), 6);
    47 }
    48 
    49 //月初
    50 function getMonthStartDate() {
    51     return new Date(nowYear, nowMonth, 1);
    52 }
    53 
    54 //月末。下月初-1天
    55 function getMonthEndDate() {
    56     return AddDays(AddMonths(getMonthStartDate(), 1), -1);
    57 }
    58 
    59 //季度初
    60 function getQuarterStartDate() {
    61     return new Date(nowYear, getQuarterStartMonth(), 1);
    62 }
    63 
    64 //季度末。下季初-1天
    65 function getQuarterEndDate() {
    66     return AddDays(AddMonths(getQuarterStartDate(), 3), -1);
    67 }
  • 相关阅读:
    python反爬之js混淆-字符串映射
    How to Provide a Default Trace for a Cloud Application
    Reset Peak Used Memory
    SAP 课程
    note 1639578 and 1622837
    SAP License error
    SAP BASIS-System Move
    初识Agile(敏捷项目管理)
    SAP HANA升级
    SAP FIORI 部署
  • 原文地址:https://www.cnblogs.com/ahdung/p/2881566.html
Copyright © 2011-2022 走看看