Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export function formatTimeDiff(dateTime) {
- const date = new Date(dateTime);
- const currentDate = new Date();
- let diff = (+currentDate - +date) / 1000;
- const days = Math.floor(diff / (3600 * 24));
- diff -= days * 86400;
- const hours = Math.floor(diff / 3600) % 24;
- diff -= hours * 3600;
- const minutes = Math.floor(diff / 60) % 60;
- diff -= minutes * 60;
- const seconds = Math.floor(diff % 60);
- let timeDiffString = '';
- if (days > 0) {
- timeDiffString += `${days} days `;
- }
- if (hours > 0 || days > 0) {
- timeDiffString += `${hours} hours `;
- }
- if (minutes > 0 || hours > 0 || days > 0) {
- timeDiffString += `${minutes} minutes `;
- }
- timeDiffString += `${seconds} seconds`;
- return `${timeDiffString} ago`;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement