gpt4 book ai didi

node.js - Nodejs的时差?

转载 作者:IT老高 更新时间:2023-10-28 23:02:17 29 4
gpt4 key购买 nike

我试图找出一种方法来获取两个日期之间的时间差(以秒为单位)。

例如,以秒为单位的差异:

2013-5-11 8:37:18

2013-5-11 10:37:18

试过了:

function timeDifference(laterdate, earlierdate) {
var difference = laterdate.getTime() - earlierdate.getTime();
var daysDifference = Math.floor(difference/1000/60/60/24);
difference -= daysDifference*1000*60*60*24
var hoursDifference = Math.floor(difference/1000/60/60);
difference -= hoursDifference*1000*60*60
var minutesDifference = Math.floor(difference/1000/60);
difference -= minutesDifference*1000*60
var secondsDifference = Math.floor(difference/1000);

return secondsDifference;
}

但这在Nodejs中不起作用,错误

getTime()

找不到函数

最佳答案

var moment = require('moment')
var startDate = moment('2013-5-11 8:73:18', 'YYYY-M-DD HH:mm:ss')
var endDate = moment('2013-5-11 10:73:18', 'YYYY-M-DD HH:mm:ss')
var secondsDiff = endDate.diff(startDate, 'seconds')
console.log(secondsDiff)

您将需要 moment.js模块

npm install -S moment

关于node.js - Nodejs的时差?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16495368/

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