将毫秒转换为时间戳" Time Ago" (59m,5d,3m等)在Javascript中

时间:2017-11-12 19:49:18

标签: javascript date

我已经看过一些使用PHP的例子,但我遇到了一些问题,因为我对PHP不太熟悉并且有时间限制。

我想创建一个时间戳,根据帖子的创建时间长短,它会显示不同的时间戳。

这就是我想要的:

  • 如果帖子的创建时间不到一秒钟,请显示:< 1m;

  • 如果在1-59分钟之前创建了帖子,则显示://分钟数// + " M&#34 ;;

  • 如果帖子的创建时间不到1-24小时,则显示://小时数// + " H&#34 ;;

  • 如果帖子是在1-6天前创建的,则显示://天数// +" d&#34 ;;

  • 如果帖子是在1-3周前创建的,则显示://周数// +" w&#34 ;;

  • 如果帖子是在1-12个月前创建的,则显示://月份数量// +" m&#34 ;;

这是我尝试使用的代码:

unique:true/false

function formatTime(timeCreated){ var periods = ["second", "minute", "hour", "day", "week", "month", "year", "decade"]; // var actualPeriods = ["m", "h", "d", "w", "m", "y"] var lengths = ["60","60","24","7","4.35","12","10"] var currentTime = Date.now() var difference = currentTime - timeCreated; for (var i = 0; difference >= lengths[i] && i < (lengths.length)-1; i++) { difference = difference /lengths[i]; } difference = Math.round(difference) return difference + periods[i] } 正在返回formatTime(1508189037313)

(1508189037313是2017年10月16日星期一16:23:57)

我在PHP中看到了一个使用它的例子

74year

但不确定如何将function time_elapsed_string($datetime, $full = false) { $now = new DateTime; $ago = new DateTime($datetime); $diff = $now->diff($ago); $diff->w = floor($diff->d / 7); $diff->d -= $diff->w * 7; $string = array( 'y' => 'year', 'm' => 'month', 'w' => 'week', 'd' => 'day', 'h' => 'hour', 'i' => 'minute', 's' => 'second', ); foreach ($string as $k => &$v) { if ($diff->$k) { $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : ''); } else { unset($string[$k]); } } if (!$full) $string = array_slice($string, 0, 1); return $string ? implode(', ', $string) . ' ago' : 'just now'; } ->转换为javascript。

任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:2)

创建一些存储各个句点的毫秒数的结构,从Date.now()中减去您感兴趣的日期,构建if结构检查以查看差异是否大于断点对于期间(从最大到最小) - 然后格式化某种输出。如果没有匹配,你可以返回一些表明它比一分钟更近的东西。

请注意,您在几分钟和几个月内使用的符号是相同的。

&#13;
&#13;
var periods = {
  month: 30 * 24 * 60 * 60 * 1000,
  week: 7 * 24 * 60 * 60 * 1000,
  day: 24 * 60 * 60 * 1000,
  hour: 60 * 60 * 1000,
  minute: 60 * 1000
};

function formatTime(timeCreated) {
  var diff = Date.now() - timeCreated;

  if (diff > periods.month) {
    // it was at least a month ago
    return Math.floor(diff / periods.month) + "m";
  } else if (diff > periods.week) {
    return Math.floor(diff / periods.week) + "w";
  } else if (diff > periods.day) {
    return Math.floor(diff / periods.day) + "d";
  } else if (diff > periods.hour) {
    return Math.floor(diff / periods.hour) + "h";
  } else if (diff > periods.minute) {
    return Math.floor(diff / periods.minute) + "m";
  }
  return "Just now";
}
console.log(formatTime(Date.now()));
console.log(formatTime(1510507151026));
console.log(formatTime(1510517051026));
console.log(formatTime(1508189037313));
&#13;
&#13;
&#13;

答案 1 :(得分:1)

做了一些更多的研究,问了正确的谷歌问题,发现This question also answered here并添加了一些编辑以满足我的需求!

function formatTime(timeCreated) {

  var diff = Math.floor((Date.now() - timeCreated) / 1000);
  var interval = Math.floor(diff / 31536000);

  if (interval >= 1) {
    return interval + "y";
  }
  interval = Math.floor(diff / 2592000);
  if (interval >= 1) {
    return interval + "m";
  }
  interval = Math.floor(diff / 604800);
  if (interval >= 1) {
    return interval + "w";
  }
  interval = Math.floor(diff / 86400);
  if (interval >= 1) {
    return interval + "d";
  }
  interval = Math.floor(diff / 3600);
  if (interval >= 1) {
    return interval + "h";
  }
  interval = Math.floor(diff / 60);
  if (interval >= 1) {
    return interval + " m";
  }
  return "<1m";
}