From 368a945826a1960751b8b804e06450fe893a3ce9 Mon Sep 17 00:00:00 2001 From: Mirlan Date: Wed, 22 Dec 2021 19:25:52 +0600 Subject: [PATCH] fix(2142): parser for case with and without tz --- src/helpers/parseDate/index.tsx | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/helpers/parseDate/index.tsx b/src/helpers/parseDate/index.tsx index e2e2f72c..37b31c28 100644 --- a/src/helpers/parseDate/index.tsx +++ b/src/helpers/parseDate/index.tsx @@ -1,9 +1,21 @@ +import endsWith from 'lodash/endsWith' import parse from 'date-fns/parse' -export const parseDate = (dateString: string, format = 'yyyy-MM-dd HH:mm:ss') => ( - parse( +const dateFormats = { + withTimezone: 'yyyy-MM-dd HH:mm:ssx', + withoutTimezone: 'yyyy-MM-dd HH:mm:ss', +} + +export const parseDate = ( + dateString: string, + format?: string, +) => { + const defaultFormat = endsWith(dateString, 'Z') + ? dateFormats.withTimezone + : dateFormats.withoutTimezone + return parse( dateString, - format, + format ?? defaultFormat, new Date(), ) -) +}