js处理友好时间函数

//获取当前时间戳
function  time() {
     return  Math.floor( new  Date().getTime() / 1000);
}
 
//友好时间显示方法  参数:时间戳
function  mdate($time) {
     var  $text =  '' ;
     var  $t = time() - parseInt($time);
     
     if  ($t == 0) {
         return  '刚刚' ;
     }
     if  ($t < 60) {
         return  $t +  '秒前' ;
     }
     if  ($t < 60 * 60) {
         return  Math.floor($t / 60) +  '分钟前' ;
     }
     if  ($t < 60 * 60 * 24) {
         return  Math.floor($t / (60 * 60)) +  '小时前' ;
     }
     if  ($t < 60 * 60 * 24 * 2) {
         return  '昨天 '  + date( 'H:i' , $time);
     }
     if  ($t < 60 * 60 * 24 * 3) {
         return  '前天 '  + date( 'H:i' , $time);
     }
     if  ($t < 60 * 60 * 24 * 30) {
         return  date( 'm月d日 H:i' , $time);
     }
     if  ($t < 60 * 60 * 24 * 365) {
         return  date( 'm月d日' , $time);
     else  {
         return  date( 'Y年m月d日' , $time);    //一年前
     }
}
 
//对时间戳进行格式化
function  date(format, timestamp) {
     var  that =  this , jsdate, f, formatChr = /\?([a-z])/gi, formatChrCb, _pad =  function (n, c) {
         if  ((n = n +  "" ).length < c) {
             return  new  Array((++c) - n.length).join( "0" ) + n;
         else  {
             return  n;
         }
     }, txt_words = [ "Sun" "Mon" "Tues" "Wednes" "Thurs" "Fri" "Satur" "January" "February" "March" "April" "May" "June" "July" "August" "September" "October" "November" "December" ], txt_ordin = {1:  "st" , 2:  "nd" , 3:  "rd" , 21:  "st" , 22:  "nd" , 23:  "rd" , 31:  "st" };
     formatChrCb =  function (t, s) {
         return  f[t] ? f[t]() : s;
     };
     f = {d:  function () {
             return  _pad(f.j(), 2);
         }, D:  function () {
             return  f.l().slice(0, 3);
         }, j:  function () {
             return  jsdate.getDate();
         }, l:  function () {
             return  txt_words[f.w()] +  'day' ;
         }, N:  function () {
             return  f.w() || 7;
         }, S:  function () {
             return  txt_ordin[f.j()] ||  'th' ;
         }, w:  function () {
             return  jsdate.getDay();
         }, z:  function () {
             var  a =  new  Date(f.Y(), f.n() - 1, f.j()), b =  new  Date(f.Y(), 0, 1);
             return  Math.round((a - b) / 864e5) + 1;
         }, W:  function () {
             var  a =  new  Date(f.Y(), f.n() - 1, f.j() - f.N() + 3), b =  new  Date(a.getFullYear(), 0, 4);
             return  1 + Math.round((a - b) / 864e5 / 7);
         }, F:  function () {
             return  txt_words[6 + f.n()];
         }, m:  function () {
             return  _pad(f.n(), 2);
         }, M:  function () {
             return  f.F().slice(0, 3);
         }, n:  function () {
             return  jsdate.getMonth() + 1;
         }, t:  function () {
             return ( new  Date(f.Y(), f.n(), 0)).getDate();
         }, L:  function () {
             var  y = f.Y(), a = y & 3, b = y % 4e2, c = y % 1e2;
             return  0 + (!a && (c || !b));
         }, o:  function () {
             var  n = f.n(), W = f.W(), Y = f.Y();
             return  Y + (n === 12 && W < 9 ? -1 : n === 1 && W > 9);
         }, Y:  function () {
             return  jsdate.getFullYear();
         }, y:  function () {
             return (f.Y() +  "" ).slice(-2);
         }, a:  function () {
             return  jsdate.getHours() > 11 ?  "pm"  "am" ;
         }, A:  function () {
             return  f.a().toUpperCase();
         }, B:  function () {
             var  H = jsdate.getUTCHours() * 36e2, i = jsdate.getUTCMinutes() * 60, s = jsdate.getUTCSeconds();
             return  _pad(Math.floor((H + i + s + 36e2) / 86.4) % 1e3, 3);
         }, g:  function () {
             return  f.G() % 12 || 12;
         }, G:  function () {
             return  jsdate.getHours();
         }, h:  function () {
             return  _pad(f.g(), 2);
         }, H:  function () {
             return  _pad(f.G(), 2);
         }, i:  function () {
             return  _pad(jsdate.getMinutes(), 2);
         }, s:  function () {
             return  _pad(jsdate.getSeconds(), 2);
         }, u:  function () {
             return  _pad(jsdate.getMilliseconds() * 1000, 6);
         }, e:  function () {
             return 'UTC' ;
         }, I:  function () {
             var  a =  new  Date(f.Y(), 0), c = Date.UTC(f.Y(), 0), b =  new  Date(f.Y(), 6), d = Date.UTC(f.Y(), 6);
             return  0 + ((a - c) !== (b - d));
         }, O:  function () {
             var  a = jsdate.getTimezoneOffset();
             return (a > 0 ?  "-"  "+" ) + _pad(Math.abs(a / 60 * 100), 4);
         }, P:  function () {
             var  O = f.O();
             return (O.substr(0, 3) +  ":"  + O.substr(3, 2));
         }, T:  function () {
             return 'UTC' ;
         }, Z:  function () {
             return -jsdate.getTimezoneOffset() * 60;
         }, c:  function () {
             return 'Y-m-d\Th:i:sP' .replace(formatChr, formatChrCb);
         }, r:  function () {
             return 'D, d M Y H:i:s O' .replace(formatChr, formatChrCb);
         }, U:  function () {
             return  jsdate.getTime() / 1000 | 0;
         }};
     this .date =  function (format, timestamp) {
         that =  this ;
         jsdate = (( typeof  timestamp ===  'undefined' ) ?  new  Date() : (timestamp  instanceof  Date) ?  new  Date(timestamp) :  new  Date(timestamp * 1000));
         return  format.replace(formatChr, formatChrCb);
     };
     return  this .date(format, timestamp);
}


使用方式
mdate(time());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值