[英]Get the number of hours between the given dates using date-fns library
My api provides me with a "datePublished: "2019-11-14T14:54:00.0000000Z" timestamp. I want to subtract this from the current time, date.now() or new Date() and get the difference in hours. I am using the date-fns v2 library我的 api 为我提供了“datePublished:”2019-11-14T14:54:00.0000000Z”时间戳。我想从当前时间中减去它,date.now() 或 new Date() 并获得小时差。我正在使用 date-fns v2 库
From the date-fns docs来自 date-fns 文档
var result = differenceInHours(
new Date(2014, 6, 2, 19, 0),
new Date(2014, 6, 2, 6, 50)
)
I am trying this:我正在尝试这个:
var result = differenceInHours(
2019-11-14T14:54:00.0000000Z,
new Date()
)
console.log(result) = NAN控制台日志(结果)= NAN
When I use the datePublished value and the current date, I get NAN.当我使用 datePublished 值和当前日期时,我得到 NAN。 Do I have to format the API date into a different format?
我是否必须将 API 日期格式化为不同的格式?
https://date-fns.org/v2.7.0/docs/differenceInHours https://date-fns.org/v2.7.0/docs/differenceInHours
That's because you are comparing string
with Date
and not Date
with Date
.那是因为您将
string
与Date
而不是Date
与Date
进行比较。
Check this:检查这个:
var result = differenceInHours(
new Date('2019-11-14T14:54:00.0000000Z'),
new Date()
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.