Javascript 错误计算getHours的日期之间的差异?

Javascript 错误计算getHours的日期之间的差异?,javascript,Javascript,我正在编写一个函数,它将帮助我记录代码块之间的运行时间 函数getTimestampdate{ 让hours=date.getHours; 让minutes=date.getMinutes; 设秒数=date.getSeconds; 返回`${hours{setTimeoutok,3000}; const finalTimestamp=performance.now; alertgetHumanReadableTimefinalTimestamp-initialTimestamp; }您正在使

我正在编写一个函数,它将帮助我记录代码块之间的运行时间

函数getTimestampdate{ 让hours=date.getHours; 让minutes=date.getMinutes; 设秒数=date.getSeconds; 返回`${hours<10?'0'+hours:hours}-${minutes<10?'0'+minutes:minutes}-${seconds<10?'0'+seconds:seconds}`; } //示例等待3秒以查看输出 异步函数{ 设d1=新日期; 设秒数=3; let interval=setIntervalfunction{ //控制台。清除; log`wait${seconds-}seconds…`; }, 1000; 让waitPromise=newpromiseOK,err=>{ setTimeoutfunction{ 新日期; },秒*1000; }; 让d2=等待承诺; //控制台。清除; 清晰间隔; console.log `运行时间:${getTimestampnew Dated2-d1}` }; 首先,不要用日期作为时间间隔。这会引起问题。其次,您应该使用performance.now,因为它用于性能分析并提供额外的精度

函数getHumanReadableTimet{ 常数小时数=Math.floort/3600000, 分钟=Math.floort/60000%60, 秒=Math.floort/1000%60; 返回`${hours}-${minutes}-${seconds}` } 异步=>{ const initialTimestamp=performance.now; 等待新的承诺ok=>{setTimeoutok,3000}; const finalTimestamp=performance.now; alertgetHumanReadableTimefinalTimestamp-initialTimestamp;
}您正在使用绝对日期作为时间间隔。永远不要那样做!你有两个小时的时间,因为日期大约是UTC 1970年1月1日午夜,你的时区是UTC+2。@D.Pardal:ugh。。。答对 了你我。。。我相信我在javascript.info上读到过这方面的内容。第一次在js中搞乱日期。。。