I am trying to format ISO dates with date-fns in Vue:
2022-01-13T14:06:33.612Z
1988-06-22T08:03:20.098Z
2021-12-03T14:50:34.060Z
to format them into 13.01.2022. There are some cases (say 10 entries) where I received the following formats:
1999-Dec-26
or
Wed Jan 05 2022 22:15:22 GMT+0100 (Central European Standard Time)
Also sometimes returns:
[Vue warn]: Error in v-on handler: "RangeError: Invalid time value"
I'm surprised that date-fns https://date-fns.org/docs/Getting-Started has no method to detect formats. Any suggestions? Thanks in advance!
convertIso (date) {
if (date !== "") {
date = new Date(date);
if (isValid(date) === true) {
return format(date, "dd.MM.yyyy"); // 2022-01-13T14:06:33.612Z
} else {
if (isDate(date)) {
const parseDate = parse(date, "yyyy-MMM-dd", new Date()); // 1999-Dec-26
const formatDate = format(parseDate, "dd.MM.yyyy");
// sometimes returns [Vue warn]: Error in v-on handler: "RangeError: Invalid time value"
return formatDate;
} else {
return "";
}
}
}
}
or
convertIso (date) {
if (date !== "") {
if (isValid(new Date(date)) === true)
return format(new Date(date), "dd.MM.yyyy"); // 2022-01-13T14:06:33.612Z
} else {
if (isDate(date)) {
const parseDate = parse(date, "yyyy-MMM-dd", new Date()); // 1999-Dec-26
const formatDate = format(parseDate, "dd.MM.yyyy");
// sometimes returns [Vue warn]: Error in v-on handler: "RangeError: Invalid time value"
return formatDate;
} else {
return "";
}
}
}
}