-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Let FormView handle initial / default data based on the schema
- Loading branch information
1 parent
2c28dd9
commit 589aa0e
Showing
2 changed files
with
116 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
export type DateFormat = 'date-time' | 'date' | 'time' | undefined | ||
|
||
export const formatDate = (dateString?: string, format: DateFormat = 'date-time'): string => { | ||
const userDateTimeOpts = Intl.DateTimeFormat().resolvedOptions() | ||
|
||
if (!dateString) { | ||
return '' | ||
} | ||
|
||
const date = dateString === 'now' ? new Date() : new Date(dateString) | ||
if (isNaN(date.getTime())) { | ||
console.error('Invalid date:', dateString) | ||
return 'Invalid date' | ||
} | ||
|
||
const formatOptions: Intl.DateTimeFormatOptions = { | ||
timeZone: userDateTimeOpts.timeZone | ||
} | ||
|
||
if (format === 'date-time') { | ||
formatOptions.dateStyle = 'short' | ||
formatOptions.timeStyle = 'short' | ||
} else if (format === 'date') { | ||
formatOptions.dateStyle = 'short' | ||
} else if (format === 'time') { | ||
formatOptions.timeStyle = 'short' | ||
} | ||
|
||
return new Intl.DateTimeFormat( | ||
userDateTimeOpts.locale, | ||
formatOptions | ||
).format(date) | ||
} | ||
|
||
|
||
export const formatDateToISO = (dateString?: string, format: DateFormat = 'date-time'): string => { | ||
if (!dateString) { | ||
return '' | ||
} | ||
|
||
const date = dateString === 'now' ? new Date() : new Date(dateString) | ||
if (isNaN(date.getTime())) { | ||
console.error('Invalid date:', dateString) | ||
return 'Invalid date' | ||
} | ||
|
||
const isoString = date.toISOString() | ||
|
||
if (format === 'date-time') { | ||
return isoString.slice(0, 19).replace('T', ' ') // YYYY-MM-DD HH:mm:ss | ||
} else if (format === 'date') { | ||
return isoString.slice(0, 10) // YYYY-MM-DD | ||
} else { | ||
return isoString.slice(11, 19) // HH:mm:ss | ||
} | ||
} |