在 JavaScript 中将 Unix 时间转换为“几分钟前”


Converting Unix time to "minutes ago" in JavaScript

我想掩盖 php 创建并存储在我们数据库中的 time() 几分钟前,当它被我们的 JavaScript 捕获时,一旦 JSON 请求。

http://media.queerdio.com/mobileDevice/?uri=nowplaying/1

如您所见,我们存储时间就像

"airtime":1382526339

我们想要把它变成

3 minutes ago

这就是我们所知道的。

我们首先需要运行这样的东西

function convert_time(ts) {
   return new Date(ts * 1000) 
}

获取通话时间并通过该函数运行它,这使得它符合我所读到的JavaScript(我可能是错的)Unix时间JavaScript

Bt 然后我不确定如何从 JavaScript 日期时间到几分钟前获取它。我们目前不使用jQuery,如果我们能继续沿着这条路走下去,那就太好了,因为这是我在完成编码之前遇到的最后一个问题。

不需要任何依赖,只需普通的旧JavaScript就足够了。要将 unix 时间戳转换为类似"X 时间前"的标签,您只需使用如下所示的内容:

function time2TimeAgo(ts) {
    // This function computes the delta between the
    // provided timestamp and the current time, then test
    // the delta for predefined ranges.
    var d=new Date();  // Gets the current time
    var nowTs = Math.floor(d.getTime()/1000); // getTime() returns milliseconds, and we need seconds, hence the Math.floor and division by 1000
    var seconds = nowTs-ts;
    // more that two days
    if (seconds > 2*24*3600) {
       return "a few days ago";
    }
    // a day
    if (seconds > 24*3600) {
       return "yesterday";
    }
    if (seconds > 3600) {
       return "a few hours ago";
    }
    if (seconds > 1800) {
       return "Half an hour ago";
    }
    if (seconds > 60) {
       return Math.floor(seconds/60) + " minutes ago";
    }
}

当然,您可以根据需要更改文本/范围。

希望这会有所帮助,我的观点是你不需要使用任何库来实现这种事情:)

我建议你Moment.js一个用于格式化日期对象的小型(8.8 kb缩小)javascript库。它运行良好,没有进一步的依赖关系。

如果您需要更多详细信息,可以使用这样的东西:

function timeAgo(someDateInThePast) {
    var result = '';
    var difference = Date.now() - someDateInThePast;
    if (difference < 5 * 1000) {
        return 'just now';
    } else if (difference < 90 * 1000) {
        return 'moments ago';
    }
    //it has minutes
    if ((difference % 1000 * 3600) > 0) {
        if (Math.floor(difference / 1000 / 60 % 60) > 0) {
            let s = Math.floor(difference / 1000 / 60 % 60) == 1 ? '' : 's';
            result = `${Math.floor(difference / 1000 / 60 % 60)} minute${s} `;
        }
    }
    //it has hours
    if ((difference % 1000 * 3600 * 60) > 0) {
        if (Math.floor(difference / 1000 / 60 / 60 % 24) > 0) {
            let s = Math.floor(difference / 1000 / 60 / 60 % 24) == 1 ? '' : 's';
            result = `${Math.floor(difference / 1000 / 60 / 60 % 24)} hour${s}${result == '' ? '' : ','} ` + result;
        }
    }
    //it has days
    if ((difference % 1000 * 3600 * 60 * 24) > 0) {
        if (Math.floor(difference / 1000 / 60 / 60 / 24) > 0) {
            let s = Math.floor(difference / 1000 / 60 / 60 / 24) == 1 ? '' : 's';
            result = `${Math.floor(difference / 1000 / 60 / 60 / 24)} day${s}${result == '' ? '' : ','} ` + result;
        }
    }
    return result + ' ago';
}
document.write(timeAgo(Date.parse('2019-10-10 13:10')));

https://github.com/carmatas/timeago

我建议使用Datejs。它扩展了浏览器的本机 Date 对象。

回应罗素的最后一条评论:

如果您使用带有 unix 时间戳(以秒为单位的纪元).js则说

moment.unix(unixtime).startOf('hour').fromNow()

否则,如果您使用毫秒时间戳(Java System.currentTimeMillis()),则说

moment(millis).startOf('hour').fromNow()

请参阅此处的文档:http://momentjs.com/docs/#/parsing/unix-offset/