customParseFormat plugin set random values when parsing invalid date
Hello,
When trying to parse some invalid date like "54/42/0000 00:00", "MM/DD/YYYY HH:mm", dayjs doesn't set the date as invalid and set a "random" value instead.
You can test here with a comparaison with moment https://codesandbox.io/s/eager-frost-x1ng0.
import moment from "moment";
import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat";
dayjs.extend(customParseFormat);
const momentDate = moment("00/00/0000 00:00", "MM/DD/YYYY HH:mm");
const dayjsDate = dayjs("00/00/0000 00:00", "MM/DD/YYYY HH:mm");
console.log("moment", momentDate, momentDate.isValid());
console.log("dayjs", dayjsDate, dayjsDate.isValid());
const otherMomentDate = moment("54/42/0000 00:00", "MM/DD/YYYY HH:mm");
const otherDayjsDate = dayjs("54/42/0000 00:00", "MM/DD/YYYY HH:mm");
console.log("other moment", otherMomentDate, otherMomentDate.isValid());
console.log("other dayjs", otherDayjsDate, otherDayjsDate.isValid());
Thank you :)
These are not 'random values' :-) In both cases dayjs is different from moment.
- in case of
00/00/0000the current date is used - in case of
54/42/0000the year is replaced by the currrent year and the day and the month create 'overflows'; this means from 'January 32th' dayjs creates 'February 1st' etc. Hence your example creates a date of '07/12/2026' (when running on '2022-06-02')
Hey thanks @BePo65 for the answer !
If it's not random values, I think it will be valuable to add it to the documentation. What do you think ?
If it's not random values, I think it will be valuable to add it to the documentation.
You are right, that should be part of the documentation. In my pr 1914 I proposed such a segment in the documentation.
Perhaps it would be a good idea to make a separate pr out of this topic.
Awesome, you made a PR with it !
Let's wait the merge of your PR then. As soon as your PR is merge, I will close this issue.
Thanks for #1914 ;)