forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdate-time.js
More file actions
158 lines (124 loc) · 3.67 KB
/
date-time.js
File metadata and controls
158 lines (124 loc) · 3.67 KB
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
const dayList = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const monthList = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec'];
const monthLongList = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];
function DateTimeFormat(locale, options) {
if (__DEV__ && locale !== 'en-US') {
console.warn('Wrong usage of DateTimeFormat. The ' + locale +' locale is not supported.');
}
this.format = function(date) {
let output;
if (options.month === 'short' &&
options.weekday === 'short' &&
options.day === '2-digit') {
output = dayList[date.getDay()] + ', ';
output += monthList[date.getMonth()] + ' ';
output += date.getDate();
} else if (options.month === 'long'
&& options.year === 'numeric') {
output = monthLongList[date.getMonth()];
output += ' ' + date.getFullYear();
} else if (__DEV__) {
console.warn('Wrong usage of DateTimeFormat');
}
return output;
};
}
module.exports = {
DateTimeFormat: DateTimeFormat,
addDays(d, days) {
const newDate = this.clone(d);
newDate.setDate(d.getDate() + days);
return newDate;
},
addMonths(d, months) {
const newDate = this.clone(d);
newDate.setMonth(d.getMonth() + months);
return newDate;
},
addYears(d, years) {
const newDate = this.clone(d);
newDate.setFullYear(d.getFullYear() + years);
return newDate;
},
clone(d) {
return new Date(d.getTime());
},
cloneAsDate(d) {
const clonedDate = this.clone(d);
clonedDate.setHours(0, 0, 0, 0);
return clonedDate;
},
getDaysInMonth(d) {
let resultDate = this.getFirstDayOfMonth(d);
resultDate.setMonth(resultDate.getMonth() + 1);
resultDate.setDate(resultDate.getDate() - 1);
return resultDate.getDate();
},
getFirstDayOfMonth(d) {
return new Date(d.getFullYear(), d.getMonth(), 1);
},
getWeekArray(d) {
let dayArray = [];
let daysInMonth = this.getDaysInMonth(d);
let daysInWeek;
let emptyDays;
let firstDayOfWeek;
let week;
let weekArray = [];
for (let i = 1; i <= daysInMonth; i++) {
dayArray.push(new Date(d.getFullYear(), d.getMonth(), i));
}
while (dayArray.length) {
firstDayOfWeek = dayArray[0].getDay();
daysInWeek = 7 - firstDayOfWeek;
emptyDays = 7 - daysInWeek;
week = dayArray.splice(0, daysInWeek);
for (let i = 0; i < emptyDays; i++) {
week.unshift(null);
}
weekArray.push(week);
}
return weekArray;
},
format(date) {
const m = date.getMonth() + 1;
const d = date.getDate();
const y = date.getFullYear();
return m + '/' + d + '/' + y;
},
isEqualDate(d1, d2) {
return d1 && d2 &&
(d1.getFullYear() === d2.getFullYear()) &&
(d1.getMonth() === d2.getMonth()) &&
(d1.getDate() === d2.getDate());
},
isBeforeDate(d1, d2) {
const date1 = this.cloneAsDate(d1);
const date2 = this.cloneAsDate(d2);
return (date1.getTime() < date2.getTime());
},
isAfterDate(d1, d2) {
const date1 = this.cloneAsDate(d1);
const date2 = this.cloneAsDate(d2);
return (date1.getTime() > date2.getTime());
},
isBetweenDates(dateToCheck, startDate, endDate) {
return (!(this.isBeforeDate(dateToCheck, startDate)) &&
!(this.isAfterDate(dateToCheck, endDate)));
},
isDateObject(d) {
return d instanceof Date;
},
monthDiff(d1, d2) {
let m;
m = (d1.getFullYear() - d2.getFullYear()) * 12;
m += d1.getMonth();
m -= d2.getMonth();
return m;
},
yearDiff(d1, d2) {
return ~~(this.monthDiff(d1, d2) / 12);
},
};