2012-04-16 87 views
1

我使用漂亮的日期js显示日期对象,如“2分钟前”,“5天前”等社交方式等。但虽然它在铬上工作,它不适用于Firefox并导致我是JavaScript的newbiew我不知道为什么。从J.Resig页面获取代码并根据需要进行修改。漂亮的日期js不能在Firefox上工作

下面是代码所有的

// Takes an ISO time and returns a string representing how 
// long ago the date represents. 
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 && "şimdi" || 
      diff < 120 && "1 dk önce" || 
      diff < 3600 && Math.floor(diff/60) + " dk önce" || 
      diff < 7200 && "1 saat önce" || 
      diff < 86400 && Math.floor(diff/3600) + " saat önce") || 
     day_diff == 1 && "Dün" || 
     day_diff < 7 && day_diff + " gün önce" || 
     day_diff < 31 && Math.ceil(day_diff/7) + " hafta önce"; 
} 

// Takes an ISO time and returns a string representing how 
// long later the date represents. 
function prettyReverseDate(time){ 
    var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")), 
     diff = ((date.getTime() - (new Date()).getTime())/1000), 
     day_diff = Math.floor(diff/86400); 

    //console.log("Day dif : " + day_diff); 

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

    //put what ever string you wanted like "Expirs in # hour/minute/day" 
    return day_diff == 0 && (
      diff < 60 && " şimdi" || 
      diff < 120 && " 1 dk" || 
      diff < 3600 && Math.floor(diff/60) + " dakika" || 
      diff < 7200 && " 1 saat" || 
      diff < 86400 && Math.floor(diff/3600) + " saat") || 
     day_diff == 1 && "1 gün" || 
     day_diff < 7 && day_diff + " gün" || 
     day_diff < 31 && Math.ceil(day_diff/7) + " hafta"; 
} 

// If jQuery is included in the page, adds a jQuery plugin to handle it as well 
if (typeof jQuery != "undefined") 
    jQuery.fn.prettyDate = function(){ 
     return this.each(function(){ 
      var date = prettyDate(this.title); 
      if (date) 
       jQuery(this).text(date); 
      else 
       jQuery(this).text("n/a"); 
     }); 
    }; 

// If jQuery is included in the page, adds a jQuery plugin to handle it as well 
if (typeof jQuery != "undefined") 
    jQuery.fn.prettyReverseDate = function(){ 
     return this.each(function(){ 
      var date = prettyReverseDate(this.title); 
      if (date) 
       jQuery(this).text(date); 
      else 
       //make parent element show expired 
       jQuery(this).parent().text("süresi doldu"); 
     }); 
    }; 

回答

0

首先使用萤火虫插件用于Firefox执行代码 或要显示的所述执行期间发生的错误消息。

当我只是复制并粘贴“prettyReverseDate”函数时出现语法错误。

用Firebug调试你的代码,直到你看不到任何错误信息。 然后检查实际的代码逻辑。

Firebug的文档:“” http://getfirebug.com/wiki/index.php/Main_Page

+0

当然,我正在使用萤火虫,但它没有显示出错误,对我来说它有点奇怪。我在linux上使用Firefox 11的版本。也许你可以帮我解决你找到的错误。 – 2012-04-16 11:01:24

+0

prettyDate在FF中工作我以前使用过它,您需要处理您的JavaScript技能并学习如何使用Firebug进行调试。 – Sam 2012-04-16 11:20:32

0

其有关时间格式的问题,即prettyReverse和漂亮的功能,服用它具有与启动附加部分删除那部分与分裂功能问题消失后。

var date = new Date(((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")).split('.')[0]),