在Angular中倒退时间的最佳方法是什么

时间:2019-05-06 03:00:56

标签: javascript angular typescript

获取应用程序中最近通知(相对于当前时间)的时间的最佳方法是5秒前,10秒前或7小时32分钟前?

换句话说,我有一个日期对象(格式为2019-03-12T10:05:32.257),例如距当前时间3时6分9秒,我想知道是否有一种干净的方法来实现幻数3、6、9,并以html显示。

2 个答案:

答案 0 :(得分:1)

我想您正在尝试找出两个Date对象之间的区别。

首先,您可以将它们转换为Date对象,然后使用getTime()将其转换为毫秒。之后,您将两个日期对象相减以获得时差,然后将其除以1000以秒为单位获得结果。

const targetDate = new Date('2019-03-12T10:05:32.257').getTime();
const current = new Date().getTime();
const differenceInSeconds = (current - targetDate) / 1000;

从那里,您可以将其转换为所需的格式(小时,分钟和秒)。

为了将它们转换为小时,分钟和秒,

const hours = Math.floor(differenceInSeconds / 3600);
const minutes = Math.floor(differenceInSeconds % 3600 / 60);
const seconds = Math.floor(differenceInSeconds % 3600 % 60);

最终结果将是这样:

const targetDate = new Date('2019-03-12T10:05:32.257').getTime();
const current = new Date().getTime();
const differenceInSeconds = (current - targetDate) / 1000;

const hours = Math.floor(differenceInSeconds / 3600);
const minutes = Math.floor(differenceInSeconds % 3600 / 60);
const seconds = Math.floor(differenceInSeconds % 3600 % 60);
const result = `${hours} hr ${minutes} min ${seconds} sec`
console.log(result);

答案 1 :(得分:1)

我认为解决此问题的方法可能更简洁,而且通用实现。

  1. 获取以秒为单位的日期对象差异作为第一步
  2. 然后检查是否适合

    • 年(除以31536000)

    • 月(除以2592000)

    • 天(除以86400)

    • 小时(除以3600)

    • 分钟(除以60)

function timesAgo(date) {

  var seconds = Math.floor((new Date() - date) / 1000); // get the diffrence of date object sent with current date time of the system time

  var interval = Math.floor(seconds / 31536000); // divide seconds by seconds in avg for a year to get years 

//conditioning based on years derived above 
  if (interval > 1) {
    return interval + " years";
  }
  interval = Math.floor(seconds / 2592000); // months check similar to years
  if (interval > 1) {
    return interval + " months";
  }
  interval = Math.floor(seconds / 86400); // days check similar to above 
  if (interval > 1) {
    return interval + " days";
  }
  interval = Math.floor(seconds / 3600); // hours check
  if (interval > 1) {
    return interval + " hours";
  }
  interval = Math.floor(seconds / 60); // minutes check 
  if (interval > 1) {
    return interval + " minutes";
  }
  return Math.floor(seconds) + " seconds"; // seconds check at the end
}


var withYears = new Date('August 19, 1999 23:15:30');
var withMonths = new Date('March 19, 2019 23:15:30');
var withDays = new Date('May 1, 2019 23:15:30');
var withPreviousDay = new Date('May 5, 2019 23:15:30');
var withHours = new Date('May 6, 2019 10:15:30');
console.log(timesAgo(withYears));
console.log(timesAgo(withMonths));
console.log(timesAgo(withDays));
console.log(timesAgo(withPreviousDay));
console.log(timesAgo(withHours));

更简单的方法如果您使用Angular and Moment来使用fromNow()函数-Link

console.log(moment([2007, 0, 29]).fromNow(true)); // 12 years

console.log(moment([2007, 0, 29]).fromNow()); // 12 years ago
    <script data-require="moment.js@*" data-semver="2.18.0" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.0/moment.min.js"></script>

如果您需要角管,请检查此times-ago-pipe