如何编写一个javascript漂亮的时间函数

How to write a javascript pretty time function

本文关键字:漂亮 javascript 时间 函数 一个 何编写      更新时间:2023-09-26
function local(a, b) {
    var c = b - a;
    if(c < 60) // seconds
    {
        document.write(Math.floor(c) + 'seconds ago');
    }
    else if(c >= 60 && c < 3600) // minutes
    {
        document.write(Math.floor(c/60) + 'minutes ago');
    }
    else if(c >= 3600 && c < 43200)  // hours
    {
        document.write(Math.floor(c/3600) + 'hours ago');
    }
    else if(c >= 43200) 
    {
        var d = new Date(a);
        document.write(d);
    }
}

只需连接。例如,而不是:

document.write('c/60 minutes ago');

试试这个:

document.write((c / 60) + ' minutes ago');

如果你想让输出上下调(四舍五入):

document.write(Math.floor(c / 60) + ' minutes ago');