Skip to content

Commit 0f5a8af

Browse files
authored
Merge pull request #162 from mcode/fix-timezone-parsing
dateTimeStamp now localizes to UTC
2 parents cb629d2 + d3099ee commit 0f5a8af

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/helpers/dateUtils.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ const logger = require('./logger');
33

44
moment.suppressDeprecationWarnings = true; // We handle invalid date formats
55
const dateFormat = 'YYYY-MM-DD';
6-
const dateTimeFormat = 'YYYY-MM-DDThh:mm:ssZ';
6+
const dateTimeFormat = 'YYYY-MM-DDTHH:mm:ssZ';
77

88
function formatDate(date) {
9-
const parsedDate = moment(date);
9+
const parsedDate = moment.utc(date);
1010
if (!parsedDate.isValid()) {
1111
logger.warn(`Invalid date provided: ${date}. Provided value will be used.`);
1212
return date; // Use the provided date rather than 'Invalid date'
@@ -16,13 +16,14 @@ function formatDate(date) {
1616
}
1717

1818
function formatDateTime(date) {
19-
const parsedDate = moment(date);
19+
const parsedDate = moment.utc(date);
2020
if (!parsedDate.isValid()) {
2121
logger.warn(`Invalid date provided: ${date}. Provided value will be used.`);
2222
return date; // Use the provided date rather than 'Invalid date'
2323
}
2424

25-
if (parsedDate.hour()) {
25+
// HACKY: If there is a minute, second, or hour, then we should treat this as a datetimestamp
26+
if (parsedDate.hour() || parsedDate.minute() || parsedDate.second()) {
2627
return parsedDate.format(dateTimeFormat);
2728
}
2829
return parsedDate.format(dateFormat);
@@ -31,4 +32,6 @@ function formatDateTime(date) {
3132
module.exports = {
3233
formatDate,
3334
formatDateTime,
35+
dateFormat,
36+
dateTimeFormat,
3437
};

test/helpers/dateUtils.test.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const moment = require('moment');
21
const { formatDate, formatDateTime } = require('../../src/helpers/dateUtils');
32

43
test('formatDate reformats date', () => {
@@ -11,9 +10,17 @@ test('formatDate does not reformat invalid date', () => {
1110
});
1211

1312
test('formatDateTime reformats date with a time if provided', () => {
14-
const currentTimeZone = moment('04/12/19').format('Z');
1513
expect(formatDateTime('04/12/19')).toEqual('2019-04-12');
16-
expect(formatDateTime('2019-04-12T08:00:00')).toEqual(`2019-04-12T08:00:00${currentTimeZone}`);
14+
expect(formatDateTime('2019-04-12T08:00:00')).toEqual('2019-04-12T08:00:00+00:00');
15+
});
16+
17+
test('formatDateTime respects timeZone information if provided', () => {
18+
const dateTimeWithZone = '2020-05-08T18:00:00+00:00';
19+
expect(formatDateTime(dateTimeWithZone)).toEqual('2020-05-08T18:00:00+00:00');
20+
const secondDate = '2020-05-08T18:00:00Z';
21+
expect(formatDateTime(secondDate)).toEqual('2020-05-08T18:00:00+00:00');
22+
const thirdDate = '2019-04-12T08:00:00+01:00';
23+
expect(formatDateTime(thirdDate)).toEqual('2019-04-12T07:00:00+00:00');
1724
});
1825

1926
test('formatDateTime does not reformat invalid date', () => {

0 commit comments

Comments
 (0)