From 9421781cc7acb46ceb16e673fd435af3ec7ddd31 Mon Sep 17 00:00:00 2001 From: Jake McDermott Date: Thu, 3 Oct 2019 20:02:14 -0400 Subject: [PATCH] Add basic date formatter --- awx/ui_next/src/util/dates.jsx | 6 ++++++ awx/ui_next/src/util/dates.test.jsx | 13 +++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 awx/ui_next/src/util/dates.jsx create mode 100644 awx/ui_next/src/util/dates.test.jsx diff --git a/awx/ui_next/src/util/dates.jsx b/awx/ui_next/src/util/dates.jsx new file mode 100644 index 0000000000..644c896562 --- /dev/null +++ b/awx/ui_next/src/util/dates.jsx @@ -0,0 +1,6 @@ +/* eslint-disable import/prefer-default-export */ +import { getLanguage } from './language'; + +export function formatDateString(dateString, lang = getLanguage(navigator)) { + return new Date(dateString).toLocaleString(lang); +} diff --git a/awx/ui_next/src/util/dates.test.jsx b/awx/ui_next/src/util/dates.test.jsx new file mode 100644 index 0000000000..90b02c1185 --- /dev/null +++ b/awx/ui_next/src/util/dates.test.jsx @@ -0,0 +1,13 @@ +import { formatDateString } from './dates'; + +describe('formatDateString', () => { + test('it returns the expected value', () => { + const lang = 'en-US'; + expect(formatDateString('', lang)).toEqual('Invalid Date'); + expect(formatDateString({}, lang)).toEqual('Invalid Date'); + expect(formatDateString(undefined, lang)).toEqual('Invalid Date'); + expect(formatDateString('2018-01-31T01:14:52.969227Z', lang)).toEqual( + '1/31/2018, 1:14:52 AM' + ); + }); +});