2012-06-26 201 views
1

所以這裏是我的目標,我想知道多久以前某個日期是一個非常可讀的字符串。例如:「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 
+0

你更新實際上讓事情用 「ECT」 不太清楚。你能更具體地說明你真正想要什麼嗎? –

+0

採取了點,問題更新。 – Fresheyeball

+0

好的,我已經更新了我對您更新規範的回答。 –

回答

2

DateJS通過包含一個名爲TimeSpan(在time.js中定義,不在基本JS文件中)的類來處理這個問題,該類爲您進行計算。它沒有做的格式,所以你需要的東西是這樣的:

function showDiff(date1, date2) { 
    var diff, rv, yrs; 

    diff = new TimeSpan(date2 - date1); 
    if (diff.days > 0) { 
     // A day or more difference 
     if (diff.days === 1) { 
      rv = "yesterday"; 
     } 
     else if (diff.days >= 365) { 
      yrs = diff.days/365; 
      if (yrs === 1) { 
       rv = "1 year ago"; 
      } 
      else { 
       rv = yrs + " years ago"; 
      } 
     } 
     else { 
      rv = diff.days + " ago"; 
     } 
    } 
    else { 
     // Less than 1 day difference 
     if (diff.hours > 0) { 
      if (diff.hours === 1) { 
       rv = "1 hour ago"; 
      } 
      else { 
       rv = diff.hours + " hours ago"; 
      } 
     } 
     else if (diff.minutes > 0) { 
      if (diff.minutes === 1) { 
       rv = "1 minute ago"; 
      } 
      else { 
       rv = diff.minutes + " minutes ago"; 
      } 
     } 
     else { 
      rv = "just now"; 
     } 

     return rv; 
    } 

顯然,這不是完美的,不處理閏年和所有,但它應該給你的想法。

+0

http://s12.postimg.org/lo8lgwuvh/Capture5.png 我不明白 –

+0

@Bumpmaster,您是否包含DateJS?這就是定義TimeSpan的地方。 –

+0

Ofcorse我做過,我沒那麼笨...... –

1

其實你可以在純JavaScript中做到這一點。

function format(num) { 
    var date = new Date(num), 
     time = []; 
    with(time) { 
     push(date.getUTCDate() - 1); 
     push(date.getUTCHours()); 
     push(date.getUTCMinutes()); 
     push(date.getUTCSeconds()); 
    } 
    if (time[0] > 29) { 
     time[0] = Math.floor(date/86400000); 
    } 
    return time[0] + " day " + time[1] + " hours " + time[2] + " mintues " + time[3] + " seconds ago"; 
} 

http://jsfiddle.net/DerekL/NqTnF/