[英]Get the difference between to dates in minutes with Date-FNS - NaN Error
[英]Updating to v2 date-fns: NaN
我正在將模塊date-fns
從 v1 更新到 v2。
這個輔助方法曾經起作用:
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;
}
在更新到模塊的 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.
這指向const exp
行。
所以我將該行更改為:
const exp = differenceInSeconds(newDateNow(), parseISO(ff));
這仍然會產生相同的錯誤,所以我將其更改為:
const exp = differenceInSeconds(parseISO(newDateNow()), parseISO(ff));
現在沒有錯誤,但是console.log(exp)
打印NaN
。
我應該如何實現這個? 我不應該更改newDateNow
方法,以便不必在parseISO()
上使用newDateNow()
嗎?
parseISO()
是一個用於解析 ISO 格式的字符串的函數,例如parseISO('2018-13-32')
,所以這里不是你需要的。
您的第一個示例應該仍然有效。 你看到它有什么行為?
如果您還沒有看過,文檔很方便: https ://date-fns.org/v2.28.0/docs/Getting-Started
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.