简体   繁体   中英

What is the best way to get time backwards in Angular

What is the best way to get time for recent notifications (relative to current time) in an application, which are 5 sec ago, 10 sec ago or 7 hr 32 min ago?

In other words, I have a Date Object (in format 2019-03-12T10:05:32.257 ) which is for example 3 hr 6 min 9 sec ago from current time, I am wondering if there is a clean way to achieve the magic numbers 3, 6, 9 and display in html.

More cleaner way and generic implementation that I see to approach this problem could be.

  1. Get the difference of date object in seconds converted as a first step
  2. Then check for whether it could be fit into

    • years(divide by 31536000)

    • months(divide by 2592000)

    • days(divide by 86400)

    • hours(divide by 3600)

    • minutes(divide by 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)); 

Easier way If your using Angular and Moment is to use fromNow() function - 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> 

If you need an Angular Pipe check this times-ago-pipe

I guess you are trying to find the difference between two Date objects.

First, you can convert them to Date objects, followed by using getTime() to get it in milliseconds. After which, you subtract both date objects to get the time difference, and then divide it by 1000 to get the results in seconds.

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

From there, you can convert it to your required format (hours, minutes, and seconds).

And in order to convert them into hours, minutes and seconds,

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

This is how the end result will be like:

 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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM