momentjs获取时差

我试图获取用户在带有 moment.js 的页面上花费的时间,但它返回了错误的值。


代码:


dateStart: any;


ngOnInit() {

  this.dateStart = moment(); // current time

}


// when user post data before being redirect

moment(moment(this.dateStart).diff(moment())).format("hh:mm:ss");

例如,上面的代码应该返回00:00:04,这意味着用户在这个页面中是为了4 seconds。但我得到了这样的东西06:59:55


有任何想法吗?


慕码人8056858
浏览 161回答 4
4回答

慕慕森

如果你只想要秒数(diff)为什么不使用带有测量参数的 momentjs 的 diff 函数(支持的是years,,,,,,,默认是毫秒monthsweeksdayshoursminutesseconds。)var a = moment([2007, 0, 29]);var b = moment([2007, 0, 28]);a.diff(b, 'days') // 1let dateStart = moment([2020, 9, 2, 12, 0, 0, 125]);let futureTime = moment();&nbsp;let secondsAgo = (futureTime.diff(dateStart, 'seconds') % 60);let minutesAgo = (futureTime.diff(dateStart, 'minutes') % 60);let hoursAgo = futureTime.diff(dateStart, 'hours');console.log('hour: ' + hoursAgo + ', minute: ' + minutesAgo + ', seconds: ' + secondsAgo)let hms = moment({&nbsp; hour: hoursAgo,&nbsp; minute: minutesAgo,&nbsp; seconds: secondsAgo});console.log(hms.format('HH:mm:ss'));<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.0/moment.min.js"></script>

慕虎7371278

我怀疑您正在将 4 秒后的日期格式化为 1970 年 1 月 1 日,并根据您的时区偏移量进行了调整。您正在寻找duration,而不是 date 。为什么要格式化它?为什么不只记录原始持续时间?您不需要时刻从另一个时间戳中减去一个时间戳。Date.now() 将为您提供当前时间戳:this.startDate = Date.now();// ...a few moments later...const duration = Date.now() - this.startDate; // number of ms between then and now

猛跑小猪

plotly.js 中没有函数将差异作为新的 plotly 对象返回。我认为做到这一点的唯一方法是以秒为单位获取时差,然后计算分钟和小时。以下应该工作。它将时差返回为String。所以秒和分钟总是在 0 到 60 之间,小时大于 0。结果意味着74:12:40用户在该页面上停留了 74 小时 12 分 40 秒。function runTest() {&nbsp; &nbsp; let startDate = moment('02.10.2020 8:00:00', 'DD.MM.YYYY HH:mm:ss');&nbsp; &nbsp; let endDate = moment('03.10.2020 10:24:38', 'DD.MM.YYYY HH:mm:ss');&nbsp; &nbsp; getDifferenceTime(startDate, endDate);}function getDifferenceTime(startDate, endDate) {&nbsp; &nbsp; // Get time difference in seconds&nbsp; &nbsp; let seconds = endDate.diff(startDate, 'seconds');&nbsp; &nbsp; let minutes = 0;&nbsp; &nbsp; let hours = 0;&nbsp; &nbsp; // Calculate number of minutes&nbsp; &nbsp; if (seconds >= 60) {&nbsp; &nbsp; &nbsp; &nbsp; minutes = Math.floor(seconds / 60);&nbsp; &nbsp; &nbsp; &nbsp; seconds -= minutes * 60;&nbsp; &nbsp; }&nbsp; &nbsp; // Calculate number of hours&nbsp; &nbsp; if (minutes >= 60) {&nbsp; &nbsp; &nbsp; &nbsp; hours = Math.floor(minutes / 60);&nbsp; &nbsp; &nbsp; &nbsp; minutes -= hours * 60;&nbsp; &nbsp; }&nbsp; &nbsp; let timeString = hours + ':' + minutes + ':' + seconds;&nbsp; &nbsp; console.log(timeString);&nbsp; &nbsp; return timeString;}您可以使用上面截取的代码对其进行测试。它应该返回26:24:38,这是您的预期输出。

慕斯709654

您可以先尝试转换为 UTC。moment.utc(moment().diff(this.dateStart)).format("HH:mm:ss.SSS"); //include millisecondslet start = moment();setTimeout(() => {&nbsp; const duration = moment.utc(moment().diff(start));&nbsp; console.log(duration.format("HH:mm:ss.SSS"));}, 1500);<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.0/moment.min.js"></script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript