forked from Mailtrain-org/mailtrain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate.js
75 lines (61 loc) · 1.45 KB
/
date.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
'use strict';
const moment = require('moment');
const birthdayYear = 2000;
const DateFormat = {
US: 'us',
EU: 'eur',
INTL: 'intl'
};
const dateFormatStrings = {
'us': 'MM/DD/YYYY',
'eur': 'DD/MM/YYYY',
'intl': 'YYYY-MM-DD'
};
const birthdayFormatStrings = {
'us': 'MM/DD',
'eur': 'DD/MM',
'intl': 'MM/DD'
};
function parseDate(format, text) {
const date = moment.utc(text, dateFormatStrings[format]);
if (date.isValid()) {
return date.toDate();
}
}
function parseBirthday(format, text) {
const fullDateStr = format === DateFormat.INTL ? birthdayYear + '-' + text : text + '-' + birthdayYear;
const date = moment.utc(fullDateStr, dateFormatStrings[format]);
if (date.isValid()) {
return date.toDate();
}
}
function formatDate(format, date) {
if (date === null) {
return '';
} else {
return moment.utc(date).format(dateFormatStrings[format]);
}
}
function formatBirthday(format, date) {
if (date === null) {
return '';
} else {
return moment.utc(date).format(birthdayFormatStrings[format]);
}
}
function getDateFormatString(format) {
return dateFormatStrings[format];
}
function getBirthdayFormatString(format) {
return birthdayFormatStrings[format];
}
module.exports = {
DateFormat,
birthdayYear,
parseDate,
parseBirthday,
formatDate,
formatBirthday,
getDateFormatString,
getBirthdayFormatString
};