转换“2011-06-23T13:20:12 + 0000”到很久以前

时间:2011-06-23 15:43:57

标签: javascript jquery date

将日期2011-06-23T13:20:12+0000转换为以下格式的“最佳”(也是最快)方法是什么?

45 minutes ago
4 hours ago
2 days ago
5 weeks ago

3 个答案:

答案 0 :(得分:6)

看一下jQuery.timeago,可能就是你要找的东西。

答案 1 :(得分:4)

John Resig到rescue

答案 2 :(得分:0)

我认为这会足够快:

function daysAgo(dt) {
    var diff = Math.floor((new Date() - dt) / 86400000);
    if (diff === 1)       
    {
        return diff + ' day ago';
    } else {
        return diff + ' days ago';
    }
}

function minsAgo(dt) {
    var diff = Math.floor((new Date() - dt) / 60000);
    if (diff === 1)       
    {
        return diff + ' minute ago';
    } else {
        return diff + ' minutes ago';
    }
}

var then = new Date('2011-06-23T13:20:12+0000');
document.write(then + '<br />');
document.write(daysAgo(then) + '<br />');
document.write(minsAgo(then));

您可以类似地编写数周和数小时的其他功能。此外,由于Math.floor调用,这些是近似值,但我认为这样就足够了。