-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate.js
155 lines (125 loc) · 2.48 KB
/
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
143
144
145
146
147
148
149
150
151
152
153
154
155
'use strict'
var monthCount = 12
var normalDaySpec = [
31, // January
28, // February
31, // March
30, // April
31, // May
30, // June
31, // July
31, // August
30, // September
31, // October
30, // November
31 // December
]
var leapDaySpec = [
31, // January
29, // February
31, // March
30, // April
31, // May
30, // June
31, // July
31, // August
30, // September
31, // October
30, // November
31 // December
]
// provide a year-specific day spec
function daySpec (year) {
return ((year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) ? leapDaySpec : normalDaySpec)
}
/* eslint-disable no-extend-native */
Date.prototype.addYear = function () {
this.setFullYear(this.getFullYear() + 1)
}
Date.prototype.addMonth = function () {
var m = this.getMonth() + 1
if (m === monthCount) {
m = 0
this.addYear()
}
this.setMonth(m)
}
Date.prototype.addDay = function () {
var spec = daySpec(this.getFullYear())
var d = this.getDate() + 1
if (d > spec[this.getMonth()]) {
d = 1
this.setDate(d)
this.addMonth()
}
this.setDate(d)
}
Date.prototype.addHour = function () {
var h = this.getHours() + 1
if (h === 24) {
h = 0
this.addDay()
}
this.setHours(h)
}
Date.prototype.addMinute = function () {
var m = this.getMinutes() + 1
if (m === 60) {
m = 0
this.addHour()
}
this.setMinutes(m)
}
Date.prototype.addSecond = function () {
var s = this.getSeconds() + 1
if (s === 60) {
s = 0
this.addMinute()
}
this.setSeconds(s)
}
Date.prototype.substractYear = function () {
this.setFullYear(this.getFullYear() - 1)
}
Date.prototype.substractMonth = function () {
var m = this.getMonth() - 1
if (m < 0) {
m = 0
this.substractYear()
}
this.setMonth(m)
}
Date.prototype.substractDay = function () {
// var spec = daySpec(this.getFullYear())
var d = this.getDate() - 1
// if (d > spec[this.getMonth()]) {
// d = 1
// this.setDate(d)
// this.substractMonth()
// }
this.setDate(d)
}
Date.prototype.substractHour = function () {
var h = this.getHours() - 1
if (h < 0) {
h = 0
this.substractDay()
}
this.setHours(h)
}
Date.prototype.substractMinute = function () {
var m = this.getMinutes() - 1
if (m < 0) {
m = 59
this.substractHour()
}
this.setMinutes(m)
}
Date.prototype.substractSecond = function () {
var s = this.getSeconds() - 1
if (s < 0) {
s = 59
this.substractMinute()
}
this.setSeconds(s)
}