我需要一个代码片段,用于将按秒数给出的时间量转换为某种人类可读的形式。该函数应该接收一个数字并输出一个像这样的字符串:
34 seconds
12 minutes
4 hours
5 days
4 months
1 year
不需要格式化,硬编码的格式就可以了。
我需要一个代码片段,用于将按秒数给出的时间量转换为某种人类可读的形式。该函数应该接收一个数字并输出一个像这样的字符串:
34 seconds
12 minutes
4 hours
5 days
4 months
1 year
不需要格式化,硬编码的格式就可以了。
function secondsToString(seconds)
{
var numyears = Math.floor(seconds / 31536000);
var numdays = Math.floor((seconds % 31536000) / 86400);
var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60;
return numyears + " years " + numdays + " days " + numhours + " hours " + numminutes + " minutes " + numseconds + " seconds";
}
在 Royi 的帮助下,我们得到了以人类可读形式输出时间间隔的代码:
function millisecondsToStr (milliseconds) {
// TIP: to find current time in milliseconds, use:
// var current_time_milliseconds = new Date().getTime();
function numberEnding (number) {
return (number > 1) ? 's' : '';
}
var temp = Math.floor(milliseconds / 1000);
var years = Math.floor(temp / 31536000);
if (years) {
return years + ' year' + numberEnding(years);
}
//TODO: Months! Maybe weeks?
var days = Math.floor((temp %= 31536000) / 86400);
if (days) {
return days + ' day' + numberEnding(days);
}
var hours = Math.floor((temp %= 86400) / 3600);
if (hours) {
return hours + ' hour' + numberEnding(hours);
}
var minutes = Math.floor((temp %= 3600) / 60);
if (minutes) {
return minutes + ' minute' + numberEnding(minutes);
}
var seconds = temp % 60;
if (seconds) {
return seconds + ' second' + numberEnding(seconds);
}
return 'less than a second'; //'just now' //or other string you like;
}
如果您对可以很好地完成这项工作的现有 javascript 库感兴趣,您可能需要查看moment.js。
更具体地说,与您的问题相关的 moment.js 是持续时间。
以下是一些如何利用它来完成任务的示例:
var duration = moment.duration(31536000);
// Using the built-in humanize function:
console.log(duration.humanize()); // Output: "9 hours"
console.log(duration.humanize(true)); // Output: "in 9 hours"
moment.js 内置了对 50 多种人类语言的支持,因此如果您使用该humanize()
方法,您将免费获得多语言支持。
如果您想显示准确的时间信息,您可以利用专门为此目的创建的 moment.js的moment-precise-range插件:
console.log(moment.preciseDiff(0, 39240754000);
// Output: 1 year 2 months 30 days 5 hours 12 minutes 34 seconds
需要注意的一件事是,当前 moment.js 不支持持续时间对象的周/天(以周为单位)。
希望这可以帮助!
根据@Royi 的回应进行了一次挥杆:
/**
* Translates seconds into human readable format of seconds, minutes, hours, days, and years
*
* @param {number} seconds The number of seconds to be processed
* @return {string} The phrase describing the amount of time
*/
function forHumans ( seconds ) {
var levels = [
[Math.floor(seconds / 31536000), 'years'],
[Math.floor((seconds % 31536000) / 86400), 'days'],
[Math.floor(((seconds % 31536000) % 86400) / 3600), 'hours'],
[Math.floor((((seconds % 31536000) % 86400) % 3600) / 60), 'minutes'],
[(((seconds % 31536000) % 86400) % 3600) % 60, 'seconds'],
];
var returntext = '';
for (var i = 0, max = levels.length; i < max; i++) {
if ( levels[i][0] === 0 ) continue;
returntext += ' ' + levels[i][0] + ' ' + (levels[i][0] === 1 ? levels[i][1].substr(0, levels[i][1].length-1): levels[i][1]);
};
return returntext.trim();
}
我的好处是没有重复的if
s,例如不会给你0 年 0 天 30 分 1 秒。
例如:
forHumans(60)
输出 1 minute
forHumans(3600)
输出 1 hour
和forHumans(13559879)
输出156 days 22 hours 37 minutes 59 seconds
尝试以下操作:
seconds = ~~(milliseconds / 1000);
minutes = ~~(seconds / 60);
hours = ~~(minutes / 60);
days = ~~(hours / 24);
weeks = ~~(days / 7);
year = ~~(days / 365);
笔记:
结论:这是一个粗鲁但又小又简单的片段:)