JS Date日期助手函数

Published on:
Tags: javascript

时间参数 sdate 可为时间格式字符串或者整型时间戳,例如:'2020-07-01 12:12:12'1656648732000

以下

1
2
>   表示输入
<· 表示输出

初始化#

1
2
3
4
5
6
7
8
9
10
var initDate = function(sdate)
{
if(
sdate == ''
|| typeof (sdate) == 'undefined'
)
return new Date();
else
return new Date(sdate);
}

获取年份#

1
2
3
4
var getYear = function(sdate)
{
return initDate(sdate).getFullYear();
}
1
2
>  getYear('2020-07-01');
<· 2020

获取月份#

1
2
3
4
var getMonth = function(sdate)
{
return initDate(sdate).getMonth() + 1;
}
1
2
>  getMonth('2020-07-01');
<· 7

获取日期#

1
2
3
4
var getDate = function(sdate)
{
return initDate(sdate).getDate();
}
1
2
>  getDate('2020-07-01');
<· 1

获取年月日#

1
2
3
4
5
6
7
8
9
var getDates = function(sdate)
{
let curdate = initDate(sdate);
let month = curdate.getMonth() + 1;
let date = curdate.getDate();
month = month < 10?'0'+month:month;
date = date < 10?'0'+date:date;
return curdate.getFullYear()+'-'+month+'-'+date;
}
1
2
>  getDates('2020-07-01');
<· "2020-07-01"

获取小时#

1
2
3
4
var getHours = function(sdate)
{
return initDate(sdate).getHours();
}
1
2
>  getHours('2020-07-01 12:12:12');
<· 12

获取分钟#

1
2
3
4
var getMinutes = function(sdate)
{
return initDate(sdate).getMinutes();
}
1
2
>  getMinutes('2020-07-01 12:12:12');
<· 12

获取秒钟#

1
2
3
4
var getSeconds = function(sdate)
{
return initDate(sdate).getSeconds();
}
1
2
>  getSeconds('2020-07-01 12:12:12');
<· 12

获取毫秒#

1
2
3
4
var getMilliseconds = function(sdate)
{
return initDate(sdate).getMilliseconds();
}
1
2
>  getMilliseconds('2020-07-01 12:12:12');
<· 0

获取时分秒#

1
2
3
4
5
6
7
8
9
10
11
var getTime = function(sdate)
{
let curdate = initDate(sdate);
let hours = curdate.getHours();
let minutes = curdate.getMinutes();
let seconds = curdate.getSeconds();
hours = hours < 10?'0'+hours:hours;
minutes = minutes < 10?'0'+minutes:minutes;
seconds = seconds < 10?'0'+seconds:seconds;
return hours+':'+minutes+':'+seconds;
}
1
2
>  getTime('2020-07-01 12:12:12');
<· "12:12:12"

获取年月日时分秒#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var getDateTime = function(sdate)
{
let curdate = initDate(sdate);
let month = curdate.getMonth() + 1;
let date = curdate.getDate();
let hours = curdate.getHours();
let minutes = curdate.getMinutes();
let seconds = curdate.getSeconds();
month = month < 10?'0'+month:month;
date = date < 10?'0'+date:date;
hours = hours < 10?'0'+hours:hours;
minutes = minutes < 10?'0'+minutes:minutes;
seconds = seconds < 10?'0'+seconds:seconds;
return curdate.getFullYear()+'-'+month+'-'+date+' '+hours+':'+minutes+':'+seconds;
}
1
2
3
4
>  getDateTime('2020-07-01 12:12:12');
<· "2020-07-01 12:12:12"
> getDateTime(1593576732000);
<· "2020-07-01 12:12:12"

获取两个日期相差的天数#

1
2
3
4
var getDiffDate = function(sdate,edate)
{
return (getTimeStamp(getDates(edate)) - getTimeStamp(getDates(sdate)))/(24*60*60*1000);
}
1
2
>  getDiffDate('2020-06-01','2020-07-01');
<· 30

获取星期#

1
2
3
4
5
6
var getDay = function(sdate)
{
let day = initDate(sdate).getDay();
if(day == 0) return 7;
else return day;
}
1
2
>  getDay('2020-07-01 12:12:12');
<· 3

获取周序号#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 第几周 week of year
var getWof = function(sdate,start)
{
if(start < 1 || start > 7) return false;
let end = start - 1; // 一周的最后一天是星期几
if(end == 0) end = 7;
let firstDate = getDates(getYear(sdate)+'');
let firstDay = getDay(firstDate); // 当年第一天是星期几
if(end < firstDay) end += 7;
let firstWeekDays = end - firstDay + 1; // 当年的第一周天数
let date = getDates(sdate);
let diffDate = getDiffDate(firstDate,date) + 1;
if(diffDate <= firstWeekDays) return 1;
let subDate = diffDate - firstWeekDays;
let weeks = Math.floor(subDate/7);
if(subDate%7) weeks++;
return weeks+1;
}
1
2
>  getWof('2020-07-01 12:12:12',6);
<· 27

获取时间戳#

1
2
3
4
var getTimeStamp = function(sdate)
{
return initDate(sdate).getTime();
}
1
2
>  getTimeStamp('2020-07-01 12:12:12');
<· 1593576732000

加/减 年份#

1
2
3
4
5
var setYear = function(sdate,nums)
{
let curdate = initDate(sdate);
return curdate.setFullYear(curdate.getFullYear() + nums);
}
1
2
3
4
>  setYear('2020-07-01 12:12:12',2);
<· 1656648732000
> getDateTime(1656648732000);
<· "2022-07-01 12:12:12"

加/减 月份#

1
2
3
4
5
var setMonth = function(sdate,nums)
{
let curdate = initDate(sdate);
return curdate.setMonth(curdate.getMonth() + nums);
}
1
2
3
4
>  setMonth('2020-07-01 12:12:12',2);
<· 1598933532000
> getDateTime(1598933532000);
<· "2020-09-01 12:12:12"

加/减 日期#

1
2
3
4
5
var setDate = function(sdate,nums)
{
let curdate = initDate(sdate);
return curdate.setDate(curdate.getDate() + nums);
}
1
2
3
4
>  setDate('2020-07-01 12:12:12',2);
<· 1593749532000
> getDateTime(1593749532000);
<· "2020-07-03 12:12:12"

加/减 小时#

1
2
3
4
5
var setHours = function(sdate,nums)
{
let curdate = initDate(sdate);
return curdate.setHours(curdate.getHours() + nums);
}
1
2
3
4
>  setHours('2020-07-01 12:12:12',2);
<· 1593583932000
> getDateTime(1593583932000);
<· "2020-07-01 14:12:12"

加/减 分钟#

1
2
3
4
5
var setMinutes = function(sdate,nums)
{
let curdate = initDate(sdate);
return curdate.setMinutes(curdate.getMinutes() + nums);
}
1
2
3
4
>  setMinutes('2020-07-01 12:12:12',2);
<· 1593576852000
> getDateTime(1593576852000);
<· "2020-07-01 12:14:12"

加/减 秒钟#

1
2
3
4
5
var setSeconds = function(sdate,nums)
{
let curdate = initDate(sdate);
return curdate.setSeconds(curdate.getSeconds() + nums);
}
1
2
3
4
>  setSeconds('2020-07-01 12:12:12',2);
<· 1593576734000
> getDateTime(1593576734000);
<· "2020-07-01 12:12:14"

是否闰年#

1
2
3
4
5
6
7
8
var isLeapYear = function(sdate)
{
let year = getYear(sdate);
if(year%100)
return year%4?0:1; // 普通年
else
return year%400?0:1; // 世纪年
}
1
2
>  isLeapYear('2020-07-02')
<· 1

当月有几天#

1
2
3
4
5
6
7
8
var getDaysInMonth = function(sdate)
{
let daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
let month = getMonth(sdate);
let days = daysInMonth[month-1];
if(month == 2 && isLeapYear(sdate)) days++; // 闰年2月
return days;
}
1
2
>  getDaysInMonth('2020-02-02')
<· 29

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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//  date-helper.js

// 实例化,参数为时间格式字符串或者整型时间戳
var initDate = function(sdate)
{
if(
sdate == ''
|| typeof (sdate) == 'undefined'
)
return new Date();
else
return new Date(sdate);
}
// 年
var getYear = function(sdate)
{
return initDate(sdate).getFullYear();
}
// 月
var getMonth = function(sdate)
{
return initDate(sdate).getMonth() + 1;
}
// 日
var getDate = function(sdate)
{
return initDate(sdate).getDate();
}
// 日期
var getDates = function(sdate)
{
let curdate = initDate(sdate);
let month = curdate.getMonth() + 1;
let date = curdate.getDate();
month = month < 10?'0'+month:month;
date = date < 10?'0'+date:date;
return curdate.getFullYear()+'-'+month+'-'+date;
}
// 时
var getHours = function(sdate)
{
return initDate(sdate).getHours();
}
// 分
var getMinutes = function(sdate)
{
return initDate(sdate).getMinutes();
}
// 秒
var getSeconds = function(sdate)
{
return initDate(sdate).getSeconds();
}
// 毫秒
var getMilliseconds = function(sdate)
{
return initDate(sdate).getMilliseconds();
}
// 时间
var getTime = function(sdate)
{
let curdate = initDate(sdate);
let hours = curdate.getHours();
let minutes = curdate.getMinutes();
let seconds = curdate.getSeconds();
hours = hours < 10?'0'+hours:hours;
minutes = minutes < 10?'0'+minutes:minutes;
seconds = seconds < 10?'0'+seconds:seconds;
return hours+':'+minutes+':'+seconds;
}
// 日期 时间
var getDateTime = function(sdate)
{
let curdate = initDate(sdate);
let month = curdate.getMonth() + 1;
let date = curdate.getDate();
let hours = curdate.getHours();
let minutes = curdate.getMinutes();
let seconds = curdate.getSeconds();
month = month < 10?'0'+month:month;
date = date < 10?'0'+date:date;
hours = hours < 10?'0'+hours:hours;
minutes = minutes < 10?'0'+minutes:minutes;
seconds = seconds < 10?'0'+seconds:seconds;
return curdate.getFullYear()+'-'+month+'-'+date+' '+hours+':'+minutes+':'+seconds;
}
// 两个日期相差的天数
var getDiffDate = function(sdate,edate)
{
return (getTimeStamp(getDates(edate)) - getTimeStamp(getDates(sdate)))/(24*60*60*1000);
}
// 星期
var getDay = function(sdate)
{
let day = initDate(sdate).getDay();
if(day == 0) return 7;
else return day;
}
// 第几周 week of year
var getWof = function(sdate,start)
{
if(start < 1 || start > 7) return false;
let end = start - 1; // 一周的最后一天是星期几
if(end == 0) end = 7;
let firstDate = getDates(getYear(sdate)+'');
let firstDay = getDay(firstDate); // 当年第一天是星期几
if(end < firstDay) end += 7;
let firstWeekDays = end - firstDay + 1; // 当年的第一周天数
let date = getDates(sdate);
let diffDate = getDiffDate(firstDate,date) + 1;
if(diffDate <= firstWeekDays) return 1;
let subDate = diffDate - firstWeekDays;
let weeks = Math.floor(subDate/7);
if(subDate%7) weeks++;
return weeks+1;
}
// 时间戳(毫秒)
var getTimeStamp = function(sdate)
{
return initDate(sdate).getTime();
}
// 加/减 年份
var setYear = function(sdate,nums)
{
let curdate = initDate(sdate);
return curdate.setFullYear(curdate.getFullYear() + nums);
}
// 加/减 月份
var setMonth = function(sdate,nums)
{
let curdate = initDate(sdate);
return curdate.setMonth(curdate.getMonth() + nums);
}
// 加/减 日期
var setDate = function(sdate,nums)
{
let curdate = initDate(sdate);
return curdate.setDate(curdate.getDate() + nums);
}
// 加/减 小时
var setHours = function(sdate,nums)
{
let curdate = initDate(sdate);
return curdate.setHours(curdate.getHours() + nums);
}

// 加/减 分钟
var setMinutes = function(sdate,nums)
{
let curdate = initDate(sdate);
return curdate.setMinutes(curdate.getMinutes() + nums);
}

// 加/减 秒钟
var setSeconds = function(sdate,nums)
{
let curdate = initDate(sdate);
return curdate.setSeconds(curdate.getSeconds() + nums);
}

// 是否闰年
var isLeapYear = function(sdate)
{
let year = getYear(sdate);
if(year%100)
return year%4?0:1; // 普通年
else
return year%400?0:1; // 世纪年
}

// 求当月有几天
var getDaysInMonth = function(sdate)
{
let daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
let month = getMonth(sdate);
let days = daysInMonth[month-1];
if(month == 2 && isLeapYear(sdate)) days++; // 闰年2月
return days;
}