开发者

Relative Time jQuery Plugin (like Twitter, FB, etc) Breaks in Safari?

Works in all br开发者_StackOverflow社区owsers except Firefox, any ideas?

(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);

In Safari, this code returns the given date which my plugin date if it fails. This could happen due to a future date or an invalid date. However, I'm not sure as the time that is given is standard YY MM DD HH:mm:ss

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


I think the string used is wrong and then stripped of '-' very wrong:

'010111' - interpreted by FF as Jan 1 1911 (US FF)

Correct format is '01/01/2011' (US FF)

I wouldn't use this format at all as each country has it's own way of showing/ parsing dates. The safest way to parse a string is probably to use:

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

but I would use a date object instead in the options and skip the string parsing to make sure the date is correct.

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

Question is about Safari but content FF?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜