forked from parse-community/parse-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateUtils.js
More file actions
165 lines (147 loc) · 3.22 KB
/
DateUtils.js
File metadata and controls
165 lines (147 loc) · 3.22 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
159
160
161
162
163
164
165
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
export const MONTHS = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
export const WEEKDAYS = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
const toString = Object.prototype.toString;
export function isDate(obj) {
return typeof obj === 'object' && toString.call(obj).indexOf('Date') > -1;
}
export function getWeekday(n) {
return WEEKDAYS[n];
}
export function getMonth(n) {
return MONTHS[n];
}
/**
* Returns the short form of a month.
* Normally, it returns the first 3 letters. Special cases are June, July, or Sept.
*/
export function shortMonth(month) {
if (month === 5 || month === 6 || month === 8) {
return MONTHS[month].substr(0, 4);
}
if (!MONTHS[month]) {
return '';
}
return MONTHS[month].substr(0, 3);
}
export function nextMonth(date) {
return new Date(date.getFullYear(), date.getMonth() + 1, 1);
}
export function prevMonth(date) {
return new Date(date.getFullYear(), date.getMonth() - 1, 1);
}
export function daysInMonth(date) {
const next = nextMonth(date);
const lastDay = new Date(next.getFullYear(), next.getMonth(), next.getDate() - 1);
return lastDay.getDate();
}
export function hoursFrom(date, delta) {
return new Date(
date.getFullYear(),
date.getMonth(),
date.getDate(),
date.getHours() + delta,
date.getMinutes()
);
}
export function daysFrom(date, delta) {
return new Date(
date.getFullYear(),
date.getMonth(),
date.getDate() + delta,
date.getHours(),
date.getMinutes()
);
}
export function monthsFrom(date, delta) {
return new Date(
date.getFullYear(),
date.getMonth() + delta,
date.getDate(),
date.getHours(),
date.getMinutes()
);
}
export function dateStringUTC(date) {
return date.toLocaleDateString('en-GB', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: false,
timeZone: 'UTC',
timeZoneName: 'short',
});
}
export function monthDayStringUTC(date) {
return `${shortMonth(date.getUTCMonth())} ${date.getUTCDate()}`;
}
/**
* Formats Date input into type {month} {dd}, {yyyy}
* @param {Date} date
* @return {String}
*/
export function yearMonthDayFormatter(date) {
return date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
});
}
export function yearMonthDayTimeFormatter(date, timeZone) {
const options = {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
hour12: false,
};
if (timeZone) {
options.timeZoneName = 'short';
}
return date.toLocaleDateString('en-US', options);
}
export function getDateMethod(local, methodName) {
if (!local) {
return methodName.replace('get', 'getUTC');
} else {
return methodName;
}
}
export function pad(number) {
let r = String(number);
if (r.length === 1) {
r = '0' + r;
}
return r;
}