[英]Updating to v2 date-fns: NaN
I'm updating the module date-fns
from v1 to v2.我正在将模块
date-fns
从 v1 更新到 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.
在更新到模块的 v2 之后,这会产生错误:
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.这指向
const exp
行。
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
.现在没有错误,但是
console.log(exp)
打印NaN
。
How should I implement this?我应该如何实现这个? Shouldn't I change the
newDateNow
method so that it's not necessary to use parseISO()
on newDateNow()
?我不应该更改
newDateNow
方法,以便不必在parseISO()
上使用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. parseISO()
是一个用于解析 ISO 格式的字符串的函数,例如parseISO('2018-13-32')
,所以这里不是你需要的。
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如果您还没有看过,文档很方便: https ://date-fns.org/v2.28.0/docs/Getting-Started
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.