所以這裏是我的目標,我想知道多久以前某個日期是一個非常可讀的字符串。例如:「4天3小時前」。我正在使用datejs,並將我的日期當作時代。Datejs獲取日期之間的差異
我知道我可以從另一個時代中減去一個時代,取出差異,然後手動計算要輸出的字符串。但這感覺就像datejs會處理的事情。然而,我沒有看到任何文檔(關於這個問題的任何js庫),它們表達了將日期對象或時期轉換爲時間量的方法。
任何想法,關於如何處理這個最好的?
UPDATE
讓我更清楚。當我說人類可讀的,這意味着
- 1天前是「昨天」
- 2天走的是「2天前」
- 30秒前是「剛纔」
- 1年零3天12秒前是 '1年前'
我最終什麼事做
prettyAgo = (ago) ->
ago = ago.getTime() if typeof ago is 'object'
diff = ((new Date()).getTime() - ago)/1000
dayDiff = Math.floor(diff/86400)
return false if isNaN(dayDiff) or dayDiff < 0
writer = ''
if dayDiff is 0
if diff < 1 then writer = 'just now'
else if diff < 60 then writer = Math.floor(diff)+' seconds ago'
else if diff < 120 then writer = '1 minute ago'
else if diff < 3600 then writer = Math.floor(diff/60)+' minutes ago'
else if diff < 7200 then writer = '1 hour ago'
else if diff < 86400 then writer = Math.floor(diff/3600)+' hours ago'
else if dayDiff is 1 then writer = 'yesterday'
else if dayDiff < 7 then writer = dayDiff+' days ago'
else if dayDiff is 7 then writer = '1 week ago'
else if dayDiff < 31 then writer = Math.ceil(dayDiff/7)+' weeks ago'
else writer = new Date(ago).toString 'MMM yyyy'
return writer
你更新實際上讓事情用 「ECT」 不太清楚。你能更具體地說明你真正想要什麼嗎? –
採取了點,問題更新。 – Fresheyeball
好的,我已經更新了我對您更新規範的回答。 –