gpt4 book ai didi

node.js - 如何使用node.js获取mongodb中createAt以来的时间?

转载 作者:太空宇宙 更新时间:2023-11-03 23:55:52 24 4
gpt4 key购买 nike

我想知道该帐户创建以来已经过去了多长时间:

console.log('TIMEYWIMEY',req.user.createdAt, new Date(), new Date() - req.user.createdAt)

这会打印出 TIMEYWIMEY 2019-05-10T16:12:40.457Z 2019-07-26T16:05:58.142Z NaN

我不明白为什么它是 NaN,它们看起来都是日期,我认为你可以将它们相减。

最佳答案

根据我对问题的理解,您不应该从有效日期中获取毫秒差异,然后进行计算。

const d1 = createdAt.getTime();
const d2 = new Date().getTime();
const diff = d2 - d1;

然后根据差异转换为您需要的单位。例如:

const days = diff/1000*60*60*24;

关于node.js - 如何使用node.js获取mongodb中createAt以来的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57223625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com