From 760ddec37e84608d55133a1c8fc987b48b8bcdab Mon Sep 17 00:00:00 2001 From: Brynjulf Risbakken Date: Fri, 8 Mar 2024 12:39:16 +0100 Subject: [PATCH 1/2] :sparkles: added option to get date with utc timezone instead of local one --- src/utils/date.test.ts | 31 +++++++++++++++++++++++++++++++ src/utils/date.ts | 24 +++++++++++++++++++----- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/utils/date.test.ts b/src/utils/date.test.ts index 58ae9a0b5..6b9dc86aa 100644 --- a/src/utils/date.test.ts +++ b/src/utils/date.test.ts @@ -438,3 +438,34 @@ test('isBetweenDates works as expected with todays date between itself', () => { const formatted = date.isBetweenDates(today, [today, today]); expect(formatted).toEqual(true); }); +describe('UTC timestamps', () => { + const utcDate = '2021-06-30T22:00:00+00:00'; + test('should accept UTC timestamps and get that date back in UTC in the format of DD. month YYYY', () => { + const formatted = date.formatDate(utcDate, { + format: 'DD. month YYYY', + useUTC: true, + }); + expect(formatted).toEqual('30. June 2021'); + }); + test('should accept UTC timestamps and get that date back in UTC in the format of DD. month', () => { + const formatted = date.formatDate(utcDate, { + format: 'DD. month', + useUTC: true, + }); + expect(formatted).toEqual('30. June'); + }); + test('should accept UTC timestamps and get that date back in UTC in the format of YYYY-MM-DD', () => { + const formatted = date.formatDate(utcDate, { + format: 'YYYY-MM-DD', + useUTC: true, + }); + expect(formatted).toEqual('2021-06-30'); + }); + test('should accept UTC timestamps and get that date back in UTC in the format of DD.MM.YY', () => { + const formatted = date.formatDate(utcDate, { + format: 'DD.MM.YY', + useUTC: true, + }); + expect(formatted).toEqual('30.06.21'); + }); +}); diff --git a/src/utils/date.ts b/src/utils/date.ts index ddf86c558..444ce4d4f 100644 --- a/src/utils/date.ts +++ b/src/utils/date.ts @@ -12,25 +12,39 @@ const formatDate = ( | 'DD.MM.YY' | 'DD. month'; month?: 'short' | 'long'; + useUTC?: boolean; } ): string => { if (date) { const dateObj = new Date(date); if (dateObj.getTime()) { if (options?.format === 'DD. month YYYY') { - const day = dateObj.toLocaleDateString('en-GB', { day: 'numeric' }); + const day = dateObj.toLocaleDateString('en-GB', { + day: 'numeric', + timeZone: options.useUTC ? 'UTC' : undefined, + }); return `${day}. ${dateObj.toLocaleString('en-GB', { month: options?.month ?? 'long', year: 'numeric', + timeZone: options.useUTC ? 'UTC' : undefined, })}`; } - const day = dateObj.getDate(); - const month = dateObj.getMonth() + 1; - const year = dateObj.getFullYear(); + + const day = options?.useUTC ? dateObj.getUTCDate() : dateObj.getDate(); + const month = options?.useUTC + ? dateObj.getUTCMonth() + 1 + : dateObj.getMonth() + 1; + const year = options?.useUTC + ? dateObj.getUTCFullYear() + : dateObj.getFullYear(); if (options?.format === 'DD. month') { - const day = dateObj.toLocaleDateString('en-GB', { day: 'numeric' }); + const day = dateObj.toLocaleDateString('en-GB', { + day: 'numeric', + timeZone: options.useUTC ? 'UTC' : undefined, + }); return `${day}. ${dateObj.toLocaleString('en-GB', { month: options.month ?? 'long', + timeZone: options.useUTC ? 'UTC' : undefined, })}`; } if (options?.format === 'YYYY-MM-DD') { From ccfc9bef70c7fd795c364202be1891a0cffd1e80 Mon Sep 17 00:00:00 2001 From: Brynjulf Risbakken Date: Fri, 8 Mar 2024 12:42:22 +0100 Subject: [PATCH 2/2] :bookmark: bumped version to 7.2.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 45538acb1..478763552 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@equinor/amplify-components", - "version": "7.1.6", + "version": "7.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@equinor/amplify-components", - "version": "7.1.6", + "version": "7.2.0", "license": "ISC", "dependencies": { "@azure/msal-browser": "3.9.0", diff --git a/package.json b/package.json index 8e7f8bdf1..ea54d508e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@equinor/amplify-components", - "version": "7.1.11", + "version": "7.2.0", "description": "Frontend Typescript components for the Amplify team", "main": "dist/esm/index.js", "types": "dist/types/index.d.ts",