2011-06-23 26 views
1

將日期2011-06-23T13:20:12+0000轉換爲以下格式的「最佳」(和最快)方式是什麼?將「2011-06-23T13:20:12 + 0000」轉換爲時間前

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

可能的重複[格式化和漂亮的日期與jQuery](http://stackoverflow.com/questions/2557672/formatting-and-pretty-printing-dates-with-jquery) –

回答

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調用而產生的近似值,但我認爲這足夠好。

+0

jsfiddle爲此:http:/ /jsfiddle.net/FishBasketGordo/dBJ2R/ – FishBasketGordo

相關問題