I'm updating the module date-fns
from v1 to v2.
This helper method used to work:
const { format, parseISO, differenceInSeconds } = require("date-fns");
const newDateNow = () => {
const date = new Date();
return format(date, process.env.DATE_FORMAT);
};
// `process.env.DATE_FORMAT` is set to `yyyy-MM-dd HH:mm:ss`
console.log(ff) //prints: Thu Jul 21 2022 14:44:56 GMT+0200 (Central European Summer Time)
console.log(gg) //prints: 3600
console.log(newDateNow()) //prints: 2022-07-21 15:32:05
const isExpired = () => {
const exp = differenceInSeconds(newDateNow(), ff);
return parseInt(gg) > exp;
}
After the update to the module's v2 this generates the error: date-fns doesn't accept strings as date arguments. Please use 'parseISO' to parse strings.
date-fns doesn't accept strings as date arguments. Please use 'parseISO' to parse strings.
This pointing to the const exp
line.
So I change that line to:
const exp = differenceInSeconds(newDateNow(), parseISO(ff));
This still generates the same error, so I change it to:
const exp = differenceInSeconds(parseISO(newDateNow()), parseISO(ff));
Now there is no error, but console.log(exp)
prints NaN
.
How should I implement this? Shouldn't I change the newDateNow
method so that it's not necessary to use parseISO()
on newDateNow()
?
parseISO()
is a function for parsing a string formatted in ISO format, eg parseISO('2018-13-32')
, so it's not what you need here.
Your first example should still be working. What behaviour are you seeing with it?
If you haven't seen already, the documentation is handy: https://date-fns.org/v2.28.0/docs/Getting-Started
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.