將日期2011-06-23T13:20:12+0000
轉換爲以下格式的「最佳」(和最快)方式是什麼?將「2011-06-23T13:20:12 + 0000」轉換爲時間前
45 minutes ago
4 hours ago
2 days ago
5 weeks ago
將日期2011-06-23T13:20:12+0000
轉換爲以下格式的「最佳」(和最快)方式是什麼?將「2011-06-23T13:20:12 + 0000」轉換爲時間前
45 minutes ago
4 hours ago
2 days ago
5 weeks ago
看看jQuery.timeago,可能是你在找什麼。
約翰Resig的到rescue
我認爲這將是速度不夠快:
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
調用而產生的近似值,但我認爲這足夠好。
jsfiddle爲此:http:/ /jsfiddle.net/FishBasketGordo/dBJ2R/ – FishBasketGordo
可能的重複[格式化和漂亮的日期與jQuery](http://stackoverflow.com/questions/2557672/formatting-and-pretty-printing-dates-with-jquery) –