-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathindex.js
85 lines (82 loc) · 1.96 KB
/
index.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
{
class Date2 {
constructor(date = new Date()) {
this.date = new Date(date - 0)
}
weekday(n) {
if (n) {
throw new Error('You can not set weekday')
}
return this._proxy('day')
}
day(n) {
return this._proxy('date', n)
}
year(n) {
return this._proxy('fullYear', n)
}
month(n) {
return this._proxy('month', n, 1)
}
get monthBeginning() {
return this.day(1)
}
get monthEnding() {
return this.month(this.month() + 1).day(0)
}
get nextMonth() {
let day = this.day()
let month = this.month()
let nextMonth = this.day(1).month(month + 1)
if (day > nextMonth.monthEnding.day()) {
return nextMonth.monthEnding
} else {
return nextMonth.day(day)
}
}
get previousMonth() {
let day = this.day()
let month = this.month()
let nextMonth = this.day(1).month(month - 1)
if (day > nextMonth.monthEnding.day()) {
return nextMonth.monthEnding
} else {
return nextMonth.day(day)
}
}
hours(n) {
return this._proxy('hours', n)
}
minutes(n) {
return this._proxy('minutes', n)
}
seconds(n) {
return this._proxy('seconds', n)
}
milliseconds(n) {
return this._proxy('milliseconds', n)
}
get clone() {
return new Date2(this.date)
}
_proxy(name, n, offset = 0) {
if (n === undefined) {
return this.date[`get${capitalize(name)}`]() + offset
} else {
let d = this.clone
d.date[`set${capitalize(name)}`](n - offset)
return d
}
}
isSameMonthAs(date) {
return this.year() === date.getFullYear() && this.month() === date.getMonth() + 1
}
isSameDayAs(date) {
return this.isSameMonthAs(date) && this.day() === date.getDate()
}
}
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}
window.Date2 = Date2
}