Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/helpers/dateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const logger = require('./logger');

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

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

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

if (parsedDate.hour()) {
// HACKY: If there is a minute, second, or hour, then we should treat this as a datetimestamp
if (parsedDate.hour() || parsedDate.minute() || parsedDate.second()) {
return parsedDate.format(dateTimeFormat);
}
return parsedDate.format(dateFormat);
Expand All @@ -31,4 +32,6 @@ function formatDateTime(date) {
module.exports = {
formatDate,
formatDateTime,
dateFormat,
dateTimeFormat,
};
13 changes: 10 additions & 3 deletions test/helpers/dateUtils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const moment = require('moment');
const { formatDate, formatDateTime } = require('../../src/helpers/dateUtils');

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

test('formatDateTime reformats date with a time if provided', () => {
const currentTimeZone = moment('04/12/19').format('Z');
expect(formatDateTime('04/12/19')).toEqual('2019-04-12');
expect(formatDateTime('2019-04-12T08:00:00')).toEqual(`2019-04-12T08:00:00${currentTimeZone}`);
expect(formatDateTime('2019-04-12T08:00:00')).toEqual('2019-04-12T08:00:00+00:00');
});

test('formatDateTime respects timeZone information if provided', () => {
const dateTimeWithZone = '2020-05-08T18:00:00+00:00';
expect(formatDateTime(dateTimeWithZone)).toEqual('2020-05-08T18:00:00+00:00');
const secondDate = '2020-05-08T18:00:00Z';
expect(formatDateTime(secondDate)).toEqual('2020-05-08T18:00:00+00:00');
const thirdDate = '2019-04-12T08:00:00+01:00';
expect(formatDateTime(thirdDate)).toEqual('2019-04-12T07:00:00+00:00');
});

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