中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

JS把指定日期轉(zhuǎn)換為幾秒前、幾小時(shí)前等格式

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

JS把指定日期轉(zhuǎn)換為幾秒前、幾小時(shí)前等格式, 轉(zhuǎn)換后的時(shí)間可讀性更強(qiáng)。

Date.prototype.toRelativeTime = function(now_threshold) {
  var delta = new Date() - this;

  now_threshold = parseInt(now_threshold, 10);

  if (isNaN(now_threshold)) {
    now_threshold = 0;
  }

  if (delta <= now_threshold) {
    return '剛剛';
  }

  var units = null;
  var conversions = {
    '毫秒': 1, // ms    -> ms
    '秒': 1000,   // ms    -> sec
    '分鐘': 60,     // sec   -> min
    '小時(shí)':   60,     // min   -> hour
    '天':    24,     // hour  -> day
    '月':  30,     // day   -> month (roughly)
    '年':   12      // month -> year
  };

  for (var key in conversions) {
    if (delta < conversions[key]) {
      break;
    } else {
      units = key; // keeps track of the selected key over the iteration
      delta = delta / conversions[key];
    }
  }

  // pluralize a unit when the difference is greater than 1.
  delta = Math.floor(delta);
  return [delta, units].join(" ");
};

示例代碼:

var d = new Date('2015-01-04 08:09:00'); alert(d.toRelativeTime()+'之前');

標(biāo)簽: 代碼

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:ftp上傳下載Java實(shí)現(xiàn)

下一篇:javascript計(jì)算兩個(gè)日期之間相差多少天