​ 项目中有很多时候需要用到时间格式、某些时间的获取,这里面主要包括了将时间字符转转Date对象,日期格式转换,本周、本月、本季度的开始和结束日期获取。

​ 可以直接将代码复制,粘贴在一个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
133
134
135
136
137
138
139
140
141
142
/**
* 获取本周、本季度、本月、上月的开始日期、结束日期
*/

//时间字符串转为Date对象 如:'2020-05-28'
function getDate(strDate){
var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/,
function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')');
return date;
}
//格式化日期:yyyy-MM-dd
function formatDate(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);
}

//获得某天所属月份的天数(参数为Date时间)
function getMonthDays(date){
var nowYear = date.getYear(); //当前年
var nowMonth = date.getMonth(); //当前月
var monthStartDate = new Date(nowYear, nowMonth, 1);
var monthEndDate = new Date(nowYear, nowMonth + 1, 1);
var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
return days;
}

//获得本季度的开始月份
function getQuarterStartMonth(nowMonth){
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;
}

//获得本周的开始日期(参数为Date)
function getWeekStartDate(date) {
var nowDayOfWeek = date.getDay(); //今天本周的第几天
var nowDay = date.getDate(); //当前日
var nowMonth = date.getMonth(); //当前月
var nowYear = date.getYear(); //当前年
nowYear += (nowYear < 2000) ? 1900 : 0; //
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
return formatDate(weekStartDate);
}

//获得本周的结束日期
function getWeekEndDate(date) {
var nowDayOfWeek = date.getDay(); //今天本周的第几天
var nowDay = date.getDate(); //当前日
var nowMonth = date.getMonth(); //当前月
var nowYear = date.getYear(); //当前年
nowYear += (nowYear < 2000) ? 1900 : 0; //
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
return formatDate(weekEndDate);
}

//获得本月的开始日期
function getMonthStartDate(date){
var nowMonth = date.getMonth(); //当前月
var nowYear = date.getYear(); //当前年
nowYear += (nowYear < 2000) ? 1900 : 0; //
var monthStartDate = new Date(nowYear, nowMonth, 1);
return formatDate(monthStartDate);
}

//获得本月的结束日期
function getMonthEndDate(date){
var nowYear = date.getYear(); //当前年
nowYear += (nowYear < 2000) ? 1900 : 0; //
var nowMonth = date.getMonth(); //当前月
var monthStartDate = new Date(nowYear, nowMonth, 1);
var monthEndDate = new Date(nowYear, nowMonth + 1, 1); //下个月1号
var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24); //计算本月的天数
var monthEndDate2 = new Date(nowYear, nowMonth, days); //本月最后一天
return formatDate(monthEndDate2);
}

//获得上月开始时间
function getLastMonthStartDate(date){
var lastMonthDate = date; //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
var lastYear = lastMonthDate.getYear();
lastYear += (lastYear < 2000) ? 1900 : 0; //
var lastMonth = lastMonthDate.getMonth();
var lastMonthStartDate = new Date(lastYear, lastMonth, 1);
return formatDate(lastMonthStartDate);
}

//获得上月结束时间
function getLastMonthEndDate(date){
var lastMonthDate = date; //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
var lastYear = lastMonthDate.getYear();
lastYear += (lastYear < 2000) ? 1900 : 0; //
var lastMonth = lastMonthDate.getMonth(); //上个月月份
var monthStartDate = new Date(lastYear, lastMonth, 1); //上个月的1号
var monthEndDate = new Date(lastYear, lastMonth + 1, 1); //这个月的1号
var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24); //上个月的天数
var lastMonthEndDate = new Date(lastYear, lastMonth, days);
return formatDate(lastMonthEndDate);
}

//获得本季度的开始日期
function getQuarterStartDate(date){
var nowYear = date.getYear(); //当前年
nowYear += (nowYear < 2000) ? 1900 : 0; //
var nowMonth = date.getMonth(); //当前月
var quarterStartDate = new Date(nowYear, getQuarterStartMonth(nowMonth), 1);
return formatDate(quarterStartDate);
}

//获得本季度的结束日期
function getQuarterEndDate(date){
var nowYear = date.getYear(); //当前年
nowYear += (nowYear < 2000) ? 1900 : 0; //
var nowMonth = date.getMonth(); //当前月
var quarterEndMonth = getQuarterStartMonth(nowMonth) + 2;
var endDate = new Date(nowYear, quarterEndMonth, 1); //上个月的1号
var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(endDate));
return formatDate(quarterStartDate);
}

最后更新: 2020年06月05日 11:41

原始链接: http://ligangit.com/2020/06/01/JS时间格式转换与获取/

× 请我吃糖~
打赏二维码