Skip to content

Commit

Permalink
date parse: keep timezone offset (#232)
Browse files Browse the repository at this point in the history
Co-authored-by: Stefano Ricci <[email protected]>
  • Loading branch information
SteRiccio and SteRiccio authored Jun 13, 2024
1 parent 6da722b commit 1073d7a
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/utils/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ const nowFormattedForStorage = (): string => formatForStorage(new Date())
const nowFormattedForExpression = (): string => formatForExpression(Date.now())

const parseISO = (dateStr: string): Date | undefined => (dateStr ? moment(dateStr).toDate() : undefined)
const parse = (dateStr: string, format: DateFormats): Date | undefined => {
const parse = (dateStr: string, format: DateFormats, keepTimeZone = true): Date | undefined => {
if (!dateStr) return undefined
if (format == DateFormats.datetimeStorage) return parseISO(dateStr)
if (keepTimeZone) return moment.parseZone(dateStr, format).toDate()
return moment(dateStr, format).toDate()
}

const isValidDateObject = (date: Date | undefined): boolean => !!date && moment(date).isValid()

const isValidDateInFormat = (dateStr: string, format: DateFormats) => {
const parsed = parse(dateStr, format)
return parsed && moment(parsed).isValid()
return isValidDateObject(parsed)
}

const convertDate = (params: {
Expand Down Expand Up @@ -140,8 +143,9 @@ const getTimezoneOffset = (): number => new Date().getTimezoneOffset()
export const Dates = {
isAfter,
isBefore,
isValidDateInFormat,
isValidDate,
isValidDateInFormat,
isValidDateObject,
isValidTime,
nowFormattedForStorage,
nowFormattedForExpression,
Expand Down

0 comments on commit 1073d7a

Please sign in to comment.