fix: handle iso date strings
This commit is contained in:
parent
3ba96feec9
commit
ca0a3e49ac
|
@ -7,6 +7,8 @@ test.each([
|
||||||
['12 oktober 2020', '2020-10-11T22:00:00.000Z'],
|
['12 oktober 2020', '2020-10-11T22:00:00.000Z'],
|
||||||
['5 oktober 2020 11:34', '2020-10-05T09:34:00.000Z'],
|
['5 oktober 2020 11:34', '2020-10-05T09:34:00.000Z'],
|
||||||
['15 oktober 2020 11:34', '2020-10-15T09:34:00.000Z'],
|
['15 oktober 2020 11:34', '2020-10-15T09:34:00.000Z'],
|
||||||
|
['2020-12-18T15:59:46.34', '2020-12-18T14:59:46.340Z'],
|
||||||
|
['2020-12-18T15:59:46.340Z', '2020-12-18T15:59:46.340Z'],
|
||||||
['This is an invalid date', undefined],
|
['This is an invalid date', undefined],
|
||||||
])('handles date parsing of %s', (input, expected) => {
|
])('handles date parsing of %s', (input, expected) => {
|
||||||
expect(parseDate(input)).toEqual(expected)
|
expect(parseDate(input)).toEqual(expected)
|
||||||
|
|
|
@ -4,6 +4,8 @@ const options = {
|
||||||
locale: 'sv',
|
locale: 'sv',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const toISOString = (date: DateTime) => date.toUTC().toISO()
|
||||||
|
|
||||||
export const parseDate = (input?: string): string | undefined => {
|
export const parseDate = (input?: string): string | undefined => {
|
||||||
if (!input) {
|
if (!input) {
|
||||||
return undefined
|
return undefined
|
||||||
|
@ -12,28 +14,34 @@ export const parseDate = (input?: string): string | undefined => {
|
||||||
const dateParse = (format: string) =>
|
const dateParse = (format: string) =>
|
||||||
DateTime.fromFormat(input, format, options)
|
DateTime.fromFormat(input, format, options)
|
||||||
|
|
||||||
|
const dateISO = DateTime.fromISO(input)
|
||||||
|
|
||||||
|
if (dateISO.isValid) {
|
||||||
|
return toISOString(dateISO)
|
||||||
|
}
|
||||||
|
|
||||||
const dateAndTime = dateParse('yyyy-MM-dd HH:mm')
|
const dateAndTime = dateParse('yyyy-MM-dd HH:mm')
|
||||||
|
|
||||||
if (dateAndTime.isValid) {
|
if (dateAndTime.isValid) {
|
||||||
return dateAndTime.toUTC().toISO()
|
return toISOString(dateAndTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
const onlyDate = dateParse('yyyy-MM-dd')
|
const onlyDate = dateParse('yyyy-MM-dd')
|
||||||
|
|
||||||
if (onlyDate.isValid) {
|
if (onlyDate.isValid) {
|
||||||
return onlyDate.toUTC().toISO()
|
return toISOString(onlyDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
const dateLongForm = dateParse('d MMMM yyyy')
|
const dateLongForm = dateParse('d MMMM yyyy')
|
||||||
|
|
||||||
if (dateLongForm.isValid) {
|
if (dateLongForm.isValid) {
|
||||||
return dateLongForm.toUTC().toISO()
|
return toISOString(dateLongForm)
|
||||||
}
|
}
|
||||||
|
|
||||||
const dateTimeLongForm = dateParse('d MMMM yyyy HH:mm')
|
const dateTimeLongForm = dateParse('d MMMM yyyy HH:mm')
|
||||||
|
|
||||||
if (dateTimeLongForm.isValid) {
|
if (dateTimeLongForm.isValid) {
|
||||||
return dateTimeLongForm.toUTC().toISO()
|
return toISOString(dateTimeLongForm)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Explicit return to satisfy ESLint
|
// Explicit return to satisfy ESLint
|
||||||
|
|
Loading…
Reference in New Issue