将Facebook created_time转换为Ago格式

时间:2016-01-18 19:28:41

标签: javascript google-chrome firefox

我试图创建一个函数timeAgo(time) facebook created_time(2016-01-15T20:31:30 + 0000)并将其转换为之前的时间。

Ex:刚才,2分钟,几小时,几天......

function timeAgo(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";
}

称之为

timeAgo(data[i].created_time);

它适用于除Firefox之外的所有浏览器。

enter image description here

火狐

它一直显示为未定义。

enter image description here

任何提示/建议都将不胜感激!

0 个答案:

没有答案