javascript - 相对时间 jQuery 插件(如 Twitter、FB 等)在 Safari 中中断?

标签 javascript jquery debugging time safari

适用于除 Firefox 之外的所有浏览器,有什么想法吗?

(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 中,如果失败,此代码将返回我的插件日期的给定日期。这可能是由于 future 日期或无效日期而发生的。但是,我不确定,因为给出的时间是标准的 YY MM DD HH:mm:ss

演示: http://jsfiddle.net/8azeT/

最佳答案

我认为使用的字符串是错误的,然后去掉 '-' 非常错误:

'010111' - 由 FF 解释为 1911 年 1 月 1 日(美国 FF)

正确格式为'01/01/2011'(美国FF)

我根本不会使用这种格式,因为每个国家/地区都有自己的显示/解析日期的方式。 解析字符串最安全的方法可能是使用:

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

但我会在选项中使用日期对象,并跳过字符串解析以确保日期正确。

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

问题是关于 Safari 但内容是 FF?

关于javascript - 相对时间 jQuery 插件(如 Twitter、FB 等)在 Safari 中中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4622393/

相关文章:

javascript - 如何按类查找元素数并在悬停时计算?

javascript - 在父元素中使用 jQuery 获取数据属性的值

javascript - 正则表达式 - 允许使用字母、撇号、句点、逗号和连字符

javascript - 如何调试 JS 调用

android - 在 Android Studio 中调试 Android BroadcastReceiver onReceive() 方法

javascript - jquery从选择值设置html不起作用

javascript - 使用 CSS 设计表格样式

Javascript 初学者 : setTimeout hide/show issue?

jquery - 在 Ionic2 中选择打开弹出窗口时如何检测事件?

angular - Augury 未显示组件状态