Guest User

Untitled

a guest
Apr 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. var timeSince = function(date) {
  2. if (typeof date !== 'object') {
  3. date = new Date(date);
  4. }
  5.  
  6. var seconds = Math.floor((new Date() - date) / 1000);
  7. var intervalType;
  8.  
  9. var interval = Math.floor(seconds / 31536000);
  10. if (interval >= 1) {
  11. intervalType = 'year';
  12. } else {
  13. interval = Math.floor(seconds / 2592000);
  14. if (interval >= 1) {
  15. intervalType = 'month';
  16. } else {
  17. interval = Math.floor(seconds / 86400);
  18. if (interval >= 1) {
  19. intervalType = 'day';
  20. } else {
  21. interval = Math.floor(seconds / 3600);
  22. if (interval >= 1) {
  23. intervalType = "hour";
  24. } else {
  25. interval = Math.floor(seconds / 60);
  26. if (interval >= 1) {
  27. intervalType = "minute";
  28. } else {
  29. interval = seconds;
  30. intervalType = "second";
  31. }
  32. }
  33. }
  34. }
  35. }
  36.  
  37. if (interval > 1 || interval === 0) {
  38. intervalType += 's';
  39. }
  40.  
  41. return interval + ' ' + intervalType;
  42. };
  43. var aDay = 24 * 60 * 60 * 1000;
  44. console.log(timeSince(new Date(Date.now() - aDay)));
  45. console.log(timeSince(new Date(Date.now() - aDay * 2)));
Add Comment
Please, Sign In to add comment