2011-01-07 9 views
1

Firefox以外のすべてのブラウザで動作しますか?相対時間jQueryプラグイン(Twitter、FBなど)Safariの中断?

(function($){ 
$.relativetime = function(options) { 
    var defaults = { 
     time:new Date(), 
     suffix:'ago', 
     prefix:'' 
    }; 

    options = $.extend(true, defaults, options); 

    //Fixes NaN in some browsers by removing dashes... 
    _dateStandardizer = function(dateString){ 
     modded_date = options.time.toString().replace(/-/g,' '); 
     return new Date(modded_date) 
    } 

    //Time object with all the times that can be used throughout 
    //the plugin and for later extensions. 
    time = { 
     unmodified:options.time, //the original time put in 
     original:_dateStandardizer(options.time).getTime(), //time that was given in UNIX time 
     current:new Date().getTime(), //time right now 
     displayed:'' //what will be shown 
    } 
    //The difference in the unix timestamps 
    time.diff = time.current-time.original; 

    //Here we save a JSON object with all the different measurements 
    //of time. "week" is not yet in use. 
    time.segments = { 
     second:time.diff/1000, 
     minute:time.diff/1000/60, 
     hour:time.diff/1000/60/60, 
     day:time.diff/1000/60/60/24, 
     week:time.diff/1000/60/60/24/7, 
     month:time.diff/1000/60/60/24/30, 
     year:time.diff/1000/60/60/24/365 
    } 

    //Takes a string and adds the prefix and suffix options around it 
    _uffixWrap = function(str){ 
     return options.prefix+' '+str+' '+options.suffix; 
    } 

    //Converts the time to a rounded int and adds an "s" if it's plural 
    _niceDisplayDate = function(str,date){ 
     _roundedDate = Math.round(date); 
     s=''; 
     if(_roundedDate !== 1){ s='s'; } 
     return _uffixWrap(_roundedDate+' '+str+s) 
    } 

    //Now we loop through all the times and find out which one is 
    //the right one. The time "days", "minutes", etc that gets 
    //shown is based on the JSON time.segments object's keys 
    for(x in time.segments){ 
     if(time.segments[x] >= 1){ 
      time.displayed = _niceDisplayDate(x,time.segments[x]) 
     } 
     else{ 
      break; 
     } 
    } 

    //If time.displayed is still blank (a bad date, future date, etc) 
    //just return the original, unmodified date. 
    if(time.displayed == ''){time.displayed = time.unmodified;} 

    //Give it to em! 
    return time.displayed; 

}; 
})(jQuery); 

Safariでは、このコードは、失敗した場合、私のプラグインの日付を返します。これは、将来の日付または無効な日付のために発生する可能性があります。

'010111': - 「」非常に間違った私が使用した文字列が間違っていると思うし、その後剥奪 http://jsfiddle.net/8azeT/

+2

jsfiddleまたはそれは起こらなかった – mpen

+0

http://jsfiddle.net/8azeT/ –

答えて

0

:しかし、私は与えられた時間は、標準YY MM DD HH:mm:ss

デモであるとわからないんだけど - 1911年1月1日(米国FF)としてFFによって解釈

正しいフォーマットである'01/01/2011'私はEAとしてこのフォーマットをまったく使用しないこと

(米国FF) ch国では、日付を表示/解析する独自の方法があります。文字列を解析する 最も安全な方法は、使用することが考えられます:

'January 1, 2011 1:30:11 pm GMT' 

が、私はオプションに代わり、日付オブジェクトを使用して日付が正しいことを確認するために解析する文字列をスキップします。

http://jsfiddle.net/8azeT/4/

質問は、Safariのが、コンテンツFFについてですか?

+0

これは、上記の形式で日付を返すCMSのプラグインです。それは私の場合は、私はあなたの例のようにそれを作るだろうと私はまだそれを行うとGithubでリリースするかもしれないが、この例では、私の構文解析の形式は、その形式でなければならない:\そしてはい、それだけですSafariで壊れました。 FFが正常に動作しています。 –

+0

FFで壊れても、100年が正しい答えでない場合、それはすべてあなたが使用しているFFのロケールに依存します。私はあなたの最善の策は、これは静的な場合は、あなたがそれを取得している形式に従ってそれを解析することだと思う - http://jsfiddle.net/8azeT/5/ – sunn0

関連する問題