cool hit counter js date calculation and quick get week, month, quarter start and end date_Intefrankly

js date calculation and quick get week, month, quarter start and end date


Coincidentally, this section contacted a little js, just started a variety of bump a variety of hands, a date calculation are tossing me half a day, the accumulation of some, hurry to code under:.  

  1 var now = new Date();                                                 //Current date
  2 var nowDayOfWeek = (now.getDay() == 0) ?  7 : now.getDay() - 1;        // Today is the first day of the week. Monday = 0, Sunday = 6
  3 var nowDay = now.getDate();                                           //current day
  4 var nowMonth = now.getMonth();                                        // Current monthly value (January = 0, December = 11)
  5 var nowMonReal = now.getMonth() + 1;                                  // Actual figures for current month
  6 var nowYear = now.getFullYear();                                      /current year
 7 
  8 // Date + day
 9 function AddDays(d, n) {
 10 var t = new Date(d);// copy and manipulate the new object to avoid altering the original object
11     t.setDate(t.getDate() + n);
12     return t;
13 }
14 
 15 // Date + month. Day to day, or the last day if the date does not exist in the target month
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 // Date + year. Month to month and day to day, or the last day if the date does not exist in the target year and month
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 // Get the start month of the quarter
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 // Monday
40 function getWeekStartDate() {
41     return AddDays(now, -nowDayOfWeek);
42 }
43 
 44 // Sunday. This Monday + 6 days
45 function getWeekEndDate() {
46     return AddDays(getWeekStartDate(), 6);
47 }
48 
 49 // Early in the month
50 function getMonthStartDate() {
51     return new Date(nowYear, nowMonth, 1);
52 }
53 
 54 // Month end. Early next month - 1 day
55 function getMonthEndDate() {
56     return AddDays(AddMonths(getMonthStartDate(), 1), -1);
57 }
58 
 59 // beginning of quarter
60 function getQuarterStartDate() {
61     return new Date(nowYear, getQuarterStartMonth(), 1);
62 }
63 
 64 // End of quarter. Early next season - 1 day
65 function getQuarterEndDate() {
66     return AddDays(AddMonths(getQuarterStartDate(), 3), -1);
67 }

Recommended>>
1、Introduction to gulpwebpack tool integration
2、C extension of function parameters
3、The Evolution of Sina Weibo Platform Automation and Maintenance
4、ssh service authentication key based authentication process explained
5、Android PopupWindow at any position

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号