JS实现获取本周、本月、本季度、半年的时间

工作类积累的一些小东西

整理记录一些关于时间的js的笔记,有获取半周,半年的啦,本月的啦等等,一些小东西,以后用起来可能方便一些,
想到的话,就不用自己没次搜索啦~~~~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
define(function (require) {
var now = new Date(); // 当前日期
var nowDayOfWeek = now.getDay(); // 今天本周的第几天
var nowDay = now.getDate(); // 当前日
var nowMonth = now.getMonth(); // 当前月
var nowYear = now.getYear(); // 当前年
nowYear += (nowYear < 2000) ? 1900 : 0;
var choiceData = {
// 格局化日期:yyyy-MM-dd
formatDate: function (date) {
var myyear = date.getFullYear();
var mymonth = date.getMonth() + 1;
var myweekday = date.getDate();
if (mymonth < 10) {
mymonth = '0' + mymonth;
}
if (myweekday < 10) {
myweekday = '0' + myweekday;
}
return (myyear + '-' + mymonth + '-' + myweekday);
},
// 获得某月的天数
getMonthDays: function (myMonth) {
var monthStartDate = new Date(nowYear, myMonth, 1);
var monthEndDate = new Date(nowYear, myMonth + 1, 1);
var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
return days;
},
// 获得本季度的开端月份
getQuarterStartMonth: function () {
var quarterStartMonth = 0;
if (nowMonth < 3) {
quarterStartMonth = 0;
}
if (2 < nowMonth && nowMonth < 6) {
quarterStartMonth = 3;
}
if (5 < nowMonth && nowMonth < 9) {
quarterStartMonth = 6;
}
if (nowMonth > 8) {
quarterStartMonth = 9;
}
return quarterStartMonth;
},
// 获得今天开始/结束时间
today: function () {
return this.formatDate(now);
},
// 获得本周的开端日期
getWeekStartDate: function () {
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
return this.formatDate(weekStartDate);
},
// 获得本周的停止日期
getWeekEndDate: function () {
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
return this.formatDate(weekEndDate);
},
// 获得本月的开端日期
getMonthStartDate: function () {
var monthStartDate = new Date(nowYear, nowMonth, 1);
return this.formatDate(monthStartDate);
},
// 获得本月的停止日期
getMonthEndDate: function () {
var monthEndDate = new Date(nowYear, nowMonth, this.getMonthDays(nowMonth));
return this.formatDate(monthEndDate);
},
// 获得本季度的开端日期
getQuarterStartDate: function () {
var quarterStartDate = new Date(nowYear, this.getQuarterStartMonth(), 1);
return this.formatDate(quarterStartDate);
},
// 获得本季度的停止日期
getQuarterEndDate: function () {
var quarterEndMonth = this.getQuarterStartMonth() + 2;
var quarterStartDate = new Date(nowYear, quarterEndMonth, this.getMonthDays(quarterEndMonth));
return this.formatDate(quarterStartDate);
},
// 获得半年开始
getQuarterHalfYearStart: function () {
var str = '';
if (nowMonth <= 6) { //
str = nowYear + '-01-01';
}
else {
str = nowYear + '-07-30';
}
return str;
},
// 获得半年结束
getQuarterHalfYearEnd: function () {
var str = '';
if (nowMonth <= 6) { //
str = nowYear + '-06-30';
}
else {
str = nowYear + '-12-31';
}
return str;
},
// 获得一年的开始时间
getQuarterYearStart: function () {
return nowYear + '-01-01';
},
// 获得一年的结束时间
getQuarterYearEnd: function () {
return nowYear + '-12-31';
}
};
return choiceData;
});

##有问题反馈
在使用中有任何问题,欢迎反馈给我,可以用以下联系方式跟我交流

文章目录
|