将以秒为单位的时间间隔转换为更易读的形式

IT技术 javascript datetime time
2021-01-23 00:18:33

我需要一个代码片段,用于将按秒数给出的时间量转换为某种人类可读的形式。该函数应该接收一个数字并输出一个像这样的字符串:

34 seconds 
12 minutes 
4 hours 
5 days 
4 months
1 year

不需要格式化,硬编码的格式就可以了。

6个回答
 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";

}
谢谢你的帮助,罗伊。请参阅下面我的答案,它完全回答了我的问题
2021-03-20 00:18:33
和神奇的数字: secondsInAYear = 31536000; secondsInADay = 86400; secondsInAnHour = 3600; secondsInAMinute = 60;
2021-03-23 00:18:33
(((seconds % 31536000) % 86400) % 3600) % 60 === seconds % 60 所以你可以节省一些会在你的 CPU 中死亡的位 var numyears = Math.floor(seconds / 31536000); var numdays = Math.floor((seconds % 31536000) / 86400); var numhours = Math.floor((seconds % 86400) / 3600); var numminutes = Math.floor((seconds % 3600) / 60); var numseconds = seconds % 60;
2021-04-02 00:18:33
注意:这不考虑闰年,仅提供作为“时间跨度”的值。
2021-04-04 00:18:33
谢谢!如果您添加复数(年/年)检查,那将是完美的!
2021-04-09 00:18:33

在 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;
}
这是错误的。对于像“3 天 0 小时 50 分 36 秒”这样的时间,它只会得到“3 天”,因为它在小时恰好为“0”时返回。
2021-03-16 00:18:33

如果您对可以很好地完成这项工作的现有 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.jsmoment-precise-range插件:

console.log(moment.preciseDiff(0, 39240754000);
// Output: 1 year 2 months 30 days 5 hours 12 minutes 34 seconds

需要注意的一件事是,当前 moment.js 不支持持续时间对象的周/天(以周为单位)。

希望这可以帮助!

这是相当不精确的。moment.duration(40, 'seconds')产量'a few seconds'moment.duration(45, 'seconds')产量'a minute'看到这个功能请求
2021-03-24 00:18:33
惊人的!非常感谢!
2021-04-11 00:18:33

根据@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();
}

我的好处是没有重复的ifs,例如不会给你0 年 0 天 30 分 1 秒

例如:

forHumans(60) 输出 1 minute

forHumans(3600) 输出 1 hour

forHumans(13559879)输出156 days 22 hours 37 minutes 59 seconds

你会如何让它更英语友好,比如forHumans(13559879)-> 156 days, 22 hours, 37 minutes, and 59 seconds这应该是公认的答案。
2021-03-25 00:18:33
现在链接坏了
2021-03-27 00:18:33

尝试以下操作:

seconds = ~~(milliseconds / 1000);
minutes = ~~(seconds / 60);
hours = ~~(minutes / 60);
days = ~~(hours / 24);
weeks = ~~(days / 7);
year = ~~(days / 365);

笔记:

  • 通常一年有 365 天。闰年有 366 天,因此您需要额外检查这是否是您的问题。
  • 夏令时的类似问题。当时间改变时,有些日子有 23 小时和大约 25 小时。

结论:这是一个粗鲁但又小又简单的片段:)

根据显示parseInt太慢的bemchmark 测试重写了您的代码
2021-03-21 00:18:33
这不起作用。例如,如果毫秒为 300000(5 分钟),那么这给出了毫秒 = 300000、秒 = 300、分钟 = 5,这不是 OP 请求的输出。 jsfiddle.net/yp6fcacs
2021-04-08 00:18:33