2013-02-21 56 views
0

我和約翰Resig的prettyDate功能發揮才能把它用這個格式的字符串工作串的工作:

'2013-01-04 20:02:28'

但到目前爲止我都沒有成功...此功能工作,只有ISO日期時間..修改prettyDate()與另一個日期時間格式

function prettyDate(time){ 
    var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")), 
     diff = (((new Date()).getTime() - date.getTime())/1000), 
     day_diff = Math.floor(diff/86400); 

    if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) 
     return; 

    return day_diff == 0 && (
      diff < 60 && "just now" || 
      diff < 120 && "1 minute ago" || 
      diff < 3600 && Math.floor(diff/60) + " minutes ago" || 
      diff < 7200 && "1 hour ago" || 
      diff < 86400 && Math.floor(diff/3600) + " hours ago") || 
     day_diff == 1 && "Yesterday" || 
     day_diff < 7 && day_diff + " days ago" || 
     day_diff < 31 && Math.ceil(day_diff/7) + " weeks ago"; 
} 

默認情況下,用我的字符串的結果是:

>prettyDate('2013-01-04 20:02:28') 
>undefined 

請問有人能幫我修改它來處理我的格式嗎?謝謝。

PD:http://ejohn.org/blog/javascript-pretty-date/

回答

1

這不是不工作的格式,它是功能return■當day_diff >= 31。截至目前day_diff將在功能運行時爲47。

從條件中刪除檢查,並添加一個條件,以處理該返回部分時day_diff超過31:

function prettyDate(time){ 
    var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")), 
     diff = (((new Date()).getTime() - date.getTime())/1000), 
     day_diff = Math.floor(diff/86400); 

    if (isNaN(day_diff) || day_diff < 0) 
     return; 

    return day_diff == 0 && (
      diff < 60 && "just now" || 
      diff < 120 && "1 minute ago" || 
      diff < 3600 && Math.floor(diff/60) + " minutes ago" || 
      diff < 7200 && "1 hour ago" || 
      diff < 86400 && Math.floor(diff/3600) + " hours ago") || 
     day_diff == 1 && "Yesterday" || 
     day_diff < 7 && day_diff + " days ago" || 
     day_diff < 31 && Math.ceil(day_diff/7) + " weeks ago" || 
     day_diff + " days ago"; 
} 
+0

呀@freejosh你是對的!非常感謝!!! – user1913878 2013-02-21 21:47:06