-
Notifications
You must be signed in to change notification settings - Fork 9
/
uxpdatetimeutils.js
215 lines (167 loc) · 4.98 KB
/
uxpdatetimeutils.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
export const UxpDateTimeUtils = {
months: [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
],
monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
daysShort: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
timeAM: 'am',
timePM: 'pm',
/**
* Tests a string to see if it can be converted into a valid Date object.
* Can be either a JS Date object or a string, such as 'Feb 8 2020' or '2/8/20'.
* @param {string} dateStr A JavaScript Date object or a string representation of a date.
* @example A representation of a valid Date object, such as 'new Date()'.
* @example 'Feb 8 2020' where the date is February 8, 2020.
* @example '2/8/20' where the date is February 8, 2020.
* returns {boolean} True if the string entered can make a valid JavaScript Date object. False otherwise.
*/
isValidDate: function (dateStr) {
//If we have an empty date string, return false.
if (!dateStr || dateStr === null) return false;
//Test what was entered
let timestamp = Date.parse(dateStr);
if (!isNaN(timestamp)) {
return true;
}
//Else, there was some other issue
return false;
},
parseDate: function (dateStr) {
if (this.isValidDate(dateStr)) {
return new Date(dateStr);
}
return undefined;
},
//Local time in Epoch seconds
getEpochSeconds: function (dateStr) {
if (!this.isValidDate(dateStr)) {
return undefined;
}
let date = new Date(dateStr);
return Math.round(date.getTime() / 1000);
},
//Full local date/time in UTC
getUtcString: function (dateStr) {
if (!this.isValidDate(dateStr)) {
return undefined;
}
let date = new Date(dateStr);
return date.toUTCString();
},
getNowFormattedDate: function () {
return this.getFormattedDate(new Date());
},
getNowFormattedTime: function () {
return this.getFormattedTime(new Date());
},
getNowFormattedDateTime: function () {
return this.getFormattedDateTime(new Date());
},
getFormattedDate: function (dateStr) {
if (!this.isValidDate(dateStr)) {
return undefined;
}
//Get the parts
let dt = new Date(dateStr);
let year = dt.getFullYear();
let date = dt.getDate();
let i = dt.getMonth(); //0-based
//Assemble the preferred format like: 'Feb 8, 2020'
let month = this.monthsShort[i];
return month + ' ' + date + ', ' + year;
},
getFormattedTime: function (dateStr) {
return this.getFormattedTimeAdvanced(dateStr, true, false);
},
getFormattedDateTime: function (dateStr) {
let dt = this.getFormattedDate(dateStr);
let time = this.getFormattedTime(dateStr);
if (dt && time) {
return dt + ', ' + time;
}
},
getFormattedDateTimeAdvanced: function (dateStr, in12HrFormat, withSeconds) {
let dt = this.getFormattedDate(dateStr);
let time = this.getFormattedTimeAdvanced(dateStr, in12HrFormat, withSeconds);
if (dt && time) {
return dt + ', ' + time;
}
},
getFormattedTimeAdvanced: function (dateStr, in12HrFormat, withSeconds) {
if (!this.isValidDate(dateStr)) {
return undefined;
}
//Get the parts
let dt = new Date(dateStr);
let hour = dt.getHours(); //0-23
let min = dt.getMinutes(); //0-59
var hourAdjusted = hour;
var minAdjusted = min;
if (in12HrFormat) {
hourAdjusted = hour > 12 ? hour - 12 : hour;
}
//Add the leading 0
if (min < 10) {
minAdjusted = '0' + minAdjusted;
}
//Assemble the time component
var time = hourAdjusted + ':' + minAdjusted;
//Add seconds?
if (withSeconds) {
var seconds = dt.getSeconds();
//Add the leading 0
if (seconds < 10) {
seconds = '0' + seconds;
}
time = time + ':' + seconds;
}
//Add am/pm?
if (in12HrFormat) {
var suffix = this.timeAM;
if (hour > 11) {
suffix = this.timePM;
}
time = time + ' ' + suffix;
}
return time;
},
getFullDate: function (dateStr) {
if (!this.isValidDate(dateStr)) {
return undefined;
}
//Get the parts
let dt = new Date(dateStr);
let year = dt.getFullYear();
let date = dt.getDate();
let i = dt.getMonth(); //0-based
//Assemble the preferred format like: 'February 8, 2020'
let month = this.months[i];
return month + ' ' + date + ', ' + year;
},
getFullDateTime: function (dateStr) {
let dt = this.getFullDate(dateStr);
let time = this.getFormattedTime(dateStr);
if (dt && time) {
return dt + ', ' + time;
}
},
getFullDateTimeAdvanced: function (dateStr, in12HrFormat, withSeconds) {
let dt = this.getFullDate(dateStr);
let time = this.getFormattedTimeAdvanced(dateStr, in12HrFormat, withSeconds);
if (dt && time) {
return dt + ', ' + time;
}
},
};