js友好的時間返回函數,js返回函數

來源:互聯網
上載者:User

js友好的時間返回函數,js返回函數

本文執行個體為大家分享了js顯示友好時間的函數代碼,供大家參考,具體內容如下

//友好的時間返回函數(如:10分鐘前)time_stamp為UNIX秒數function friendly_time(time_stamp){  var now_d = new Date();  var now_time = now_d.getTime() / 1000; //擷取目前時間的秒數  var f_d = new Date();  f_d.setTime(time_stamp * 1000);  var f_time = f_d.toLocaleDateString();  var ct = now_time - time_stamp;  var day = 0;  if (ct < 0)  {    f_time = "【預約】" + f_d.toLocaleString();  }  else if (ct < 60)  {    f_time = Math.floor(ct) + '秒前';  }  else if (ct < 3600)  {    f_time = Math.floor(ct / 60) + '分鐘前';  }  else if (ct < 86400)//一天  {    f_time = Math.floor(ct / 3600) + '小時前';  }  else if (ct < 604800)//7天  {    day = Math.floor(ct / 86400);    if (day < 2)      f_time = '昨天';    else      f_time = day + '天前';  }  else  {    day = Math.floor(ct / 86400);    f_time = day + '天前';  }  return f_time;}function getd(time1){  var da = new Date();  sda = new Date(time1);  var time2 = da.getTime();  var time = 0;  if (time1 > time2)  {    time = time1 - time2;    sda = da;  } else  {    time = time2 - time1;  }  if (time < 1000) return "剛剛";  time = parseInt(time / 1000);  if (time > 86400)  {    var day = parseInt(time / (24 * 60 * 60));    if (day == 1)    {      return "昨天(" + sda.getHours() + ":" + sda.getMinutes() + ")";    } else if (day < 30)    {      return day + "天前";    } else if (day < 360)    {      var moth = parseInt(day / 30);      return moth + "個月前";    } else    {      var year = parseInt(day / 360);      return year + "年前";    };  } else if (time > 3600)  {    var hour = parseInt(time / (60 * 60));    return hour + "小時前";  } else if (time > 60)  {    var hour = parseInt(time / 60);    return hour + "分鐘前";  } else  {    return time + "秒前";  }} 

有時win7系統會把時間自動顯示為帶星期的格式,需要對時間格式處理 

var rg = new RegExp("[星期一二三四五六日]","g");var q = obj_datetine.replace(rg, "").replace("-","/");//obj_datetine為要處理的時間參數 var d = new Date(Date.parse(q)); return friendly_time(d.getTime() / 1000);

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.