Guest User

Untitled

a guest
Aug 25th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. // Epochs
  2. const epochs = [
  3. ['year', 31536000],
  4. ['month', 2592000],
  5. ['day', 86400],
  6. ['hour', 3600],
  7. ['minute', 60],
  8. ['second', 1]
  9. ];
  10.  
  11.  
  12. // Get duration
  13. const getDuration = (timeAgoInSeconds) => {
  14. for (let [name, seconds] of epochs) {
  15. const interval = Math.floor(timeAgoInSeconds / seconds);
  16.  
  17. if (interval >= 1) {
  18. return {
  19. interval: interval,
  20. epoch: name
  21. };
  22. }
  23. }
  24. };
  25.  
  26.  
  27. // Calculate
  28. const timeAgo = (date) => {
  29. const timeAgoInSeconds = Math.floor((new Date() - new Date(date)) / 1000);
  30. const {interval, epoch} = getDuration(timeAgoInSeconds);
  31. const suffix = interval === 1 ? '' : 's';
  32.  
  33. return `${interval} ${epoch}${suffix} ago`;
  34. };
Add Comment
Please, Sign In to add comment