简体   繁体   English

Javascript将当前时间转换为timeago

[英]Javascript convert current time to timeago

I have been trying to convert the current time to timeago like facebook and twitter have (2 min ago, 30sec ago). 我一直在尝试将当前时间转换为timeago,例如facebook和twitter(2分钟前,30秒前)。 For that, I am taking the current time and with the help of function converting it to approx time. 为此,我正在考虑当前时间,并借助将其转换为大约时间的功能。 The code is below: 代码如下:

 var current = new Date(); console.log(timeDifference(current, new Date(2018, 03, 27, 10, 30, 00, 00))); console.log(timeDifference(current, new Date(2018, 03, 27, 10, 00, 00, 00))); function timeDifference(current, previous) { var msPerMinute = 60 * 1000; var msPerHour = msPerMinute * 60; var msPerDay = msPerHour * 24; var msPerMonth = msPerDay * 30; var msPerYear = msPerDay * 365; var elapsed = current - previous; if (elapsed < msPerMinute) { return Math.round(elapsed / 1000) + ' seconds ago'; } else if (elapsed < msPerHour) { return Math.round(elapsed / msPerMinute) + ' minutes ago'; } else if (elapsed < msPerDay) { return Math.round(elapsed / msPerHour) + ' hours ago'; } else if (elapsed < msPerMonth) { return 'approximately ' + Math.round(elapsed / msPerDay) + ' days ago'; } else if (elapsed < msPerYear) { return 'approximately ' + Math.round(elapsed / msPerMonth) + ' months ago'; } else { return 'approximately ' + Math.round(elapsed / msPerYear) + ' years ago'; } } 

But, it is not working correctly and I can't seem to know why? 但是,它无法正常工作,我似乎不知道为什么?

Your month is off by one (0 based). 您的月份减少一(以0为基数)。 (May still return wrong result based on time zones of course.) (当然,根据时区可能仍会返回错误结果。)

 var current = new Date(); console.log(timeDifference(current, new Date(2018, 02, 27, 10, 30, 00, 00))); console.log(timeDifference(current, new Date(2018, 02, 27, 10, 00, 00, 00))); function timeDifference(current, previous) { var msPerMinute = 60 * 1000; var msPerHour = msPerMinute * 60; var msPerDay = msPerHour * 24; var msPerMonth = msPerDay * 30; var msPerYear = msPerDay * 365; var elapsed = current - previous; if (elapsed < msPerMinute) { return Math.round(elapsed / 1000) + ' seconds ago'; } else if (elapsed < msPerHour) { return Math.round(elapsed / msPerMinute) + ' minutes ago'; } else if (elapsed < msPerDay) { return Math.round(elapsed / msPerHour) + ' hours ago'; } else if (elapsed < msPerMonth) { return 'approximately ' + Math.round(elapsed / msPerDay) + ' days ago'; } else if (elapsed < msPerYear) { return 'approximately ' + Math.round(elapsed / msPerMonth) + ' months ago'; } else { return 'approximately ' + Math.round(elapsed / msPerYear) + ' years ago'; } } 

The problem is in your tests as month starts with 0. Change 问题出在您的测试中,因为月份从0开始。

console.log(timeDifference(current, new Date(2018, 03, 27, 10, 30, 00, 00)));
console.log(timeDifference(current, new Date(2018, 03, 27, 10, 00, 00, 00)));

to

console.log(timeDifference(current, new Date(2018, 02, 27, 10, 30, 00, 00)));
console.log(timeDifference(current, new Date(2018, 02, 27, 10, 00, 00, 00)));

and your code works fine. 并且您的代码可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM