-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdateUtils.js
More file actions
57 lines (43 loc) · 1.35 KB
/
dateUtils.js
File metadata and controls
57 lines (43 loc) · 1.35 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
/* global ProcessMaker*/
import moment from 'moment-timezone';
moment.tz.setDefault(getTimezone())
const startsWithNumbers = /^\d{4}-/;
function getProcessMakerUserValue(key) {
if (typeof ProcessMaker !== 'undefined' && ProcessMaker.user) {
return ProcessMaker.user[key];
}
}
export function getTimezone() {
return getProcessMakerUserValue('timezone') || moment.tz.guess();
}
export function getLang() {
return getProcessMakerUserValue('lang') || 'en';
}
export function getUserDateFormat() {
if (typeof ProcessMaker !== 'undefined' && ProcessMaker.user && ProcessMaker.user.datetime_format) {
return ProcessMaker.user.datetime_format.replace(/[\sHh:msaAzZ]/g, '');
}
return 'MM/DD/YYYY';
}
export function getUserDateTimeFormat() {
return getProcessMakerUserValue('datetime_format') || 'MM/DD/YYYY h:mm A';
}
export function isValidDate(date, format = getUserDateFormat()) {
return moment(date, format).isValid();
}
export function formatIfDate(string) {
let d;
// Quick check for performance before calling moment
if (!startsWithNumbers.test(string)) {
return string;
}
d = moment(string, 'YYYY-MM-DDTHH:mm:ss.SSSZ', true);
if (d.isValid()) {
return d.format(getUserDateTimeFormat());
}
d = moment(string, 'YYYY-MM-DD', true);
if (d.isValid()) {
return d.format(getUserDateFormat());
}
return string;
}