Skip to content

Commit df95339

Browse files
committed
Update date tests
1 parent d8d5e2f commit df95339

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

shared/dates.js

+10-23
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1+
// TODO: momentjs is no longer supported. Replace with another date utility/practice. See https://momentjs.com/docs/#/-project-status/.
12
const moment = require('moment');
23

3-
const convertDateToString = (date, format = 'DD-MM-YYYY') => {
4+
const convertDateToString = (date, format = 'DD-MM-YYYY', { locale } = {}) => {
45
if (!date) return '';
6+
if (locale) setLocale(locale);
57
return moment.utc(date).format(format);
68
};
79

8-
const convertStringToDate = (date, format = 'DD-MM-YYYY') => {
10+
const convertStringToDate = (date, format = 'DD-MM-YYYY', { locale } = {}) => {
11+
if (locale) setLocale(locale);
912
return moment.utc(date, format).toDate();
1013
};
1114

1215
const convertStringFormatToAnotherFormat = (
1316
date,
1417
fromFormat = 'DD-MM-YYYY',
15-
toFormat = 'MM-DD-YYYY'
18+
toFormat = 'MM-DD-YYYY',
19+
{ locale } = {}
1620
) => {
21+
if (locale) setLocale(locale);
1722
return moment.utc(date, fromFormat).format(toFormat);
1823
};
1924

@@ -32,31 +37,13 @@ const checkDaysBetweenDates = (date, otherDate) => {
3237
return Math.ceil(hours / 24);
3338
};
3439

35-
const gitUpdatedDateToString = (dateString, locale = 'default') => {
36-
const lc = (pattern, string) =>
37-
string.replace(pattern, pattern.toLowerCase());
38-
const timeZone = 'UTC';
39-
const options = { month: 'short' };
40-
41-
const date = new Date(dateString);
42-
const month = date.toLocaleString(locale, options);
43-
const day = date.getDate();
44-
const year = date.getFullYear();
45-
const time = date
46-
.toLocaleTimeString(locale, { timeZone: timeZone })
47-
.replace(/\s/g, ' ');
48-
49-
const timeStamp = `${month} ${day}, ${year} at ${time} ${timeZone}`;
50-
51-
return lc('PM', lc('AM', timeStamp));
52-
};
40+
const setLocale = (locale = 'en') => moment.locale(locale);
5341

5442
module.exports = {
5543
convertDateToString,
5644
convertStringToDate,
5745
convertStringFormatToAnotherFormat,
5846
isValidDate,
5947
isAfterYear,
60-
checkDaysBetweenDates,
61-
gitUpdatedDateToString
48+
checkDaysBetweenDates
6249
};

0 commit comments

Comments
 (0)