Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. export function formatTimeDiff(dateTime) {
  2. const date = new Date(dateTime);
  3. const currentDate = new Date();
  4. let diff = (+currentDate - +date) / 1000;
  5.  
  6. const days = Math.floor(diff / (3600 * 24));
  7. diff -= days * 86400;
  8.  
  9. const hours = Math.floor(diff / 3600) % 24;
  10. diff -= hours * 3600;
  11.  
  12. const minutes = Math.floor(diff / 60) % 60;
  13. diff -= minutes * 60;
  14.  
  15. const seconds = Math.floor(diff % 60);
  16.  
  17. let timeDiffString = '';
  18. if (days > 0) {
  19. timeDiffString += `${days} days `;
  20. }
  21. if (hours > 0 || days > 0) {
  22. timeDiffString += `${hours} hours `;
  23. }
  24. if (minutes > 0 || hours > 0 || days > 0) {
  25. timeDiffString += `${minutes} minutes `;
  26. }
  27. timeDiffString += `${seconds} seconds`;
  28. return `${timeDiffString} ago`;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement