0

I am trying to take a string in the form of '018-09-06T15:06:44.091Z' and then subtract Date.now() from it and then convert it into a days, hours, minutes, or seconds ago string. For example, I get the string above and then: var date = Date.now() - new Date('018-09-06T15:06:44.091Z'). That gives me a number: 697850577. I need to convert that number into a string that reads '3 days ago', or '30 seconds ago', based on how much time has lapsed since '018-09-06T15:06:44.091Z'. I cannot use moment.js or any other libraries. I can only use Angular.JS 1.7 and/or vanilla JS.

Current implementation works, but there has to be a better way than this:

function conversions() {
    notif.rollupList.rollups.forEach(function (rollup) {
      rollup.type = getChangeType(rollup.type);
      rollup.modifiedAt = convertDate(rollup.modifiedAt)
    })
  }

  // Converts the date to 'something something ago'
  function convertDate(dateString) {
    var seconds = Math.floor((Date.now() - new Date(dateString)) / 
1000);
    if (seconds >= 60) {
      var minutes = Math.floor(seconds / 60);
      if (minutes >= 60) {
        var hours = Math.floor(minutes / 60);
        if (hours >= 24) {
          var days = Math.floor(hours / 24);
          if (days > 1) {
            return days.toString() + ' days ago'
          } else {
            return days.toString() + ' day ago'
          }
        } else {
          if (hours > 1) {
            return hours.toString() + ' hours ago'
          } else {
            return hours.toString() + ' hour ago'
          }
        }
      } else {
        if (minutes > 1) {
          return minutes.toString() + ' minutes ago'
        } else {
          return minutes.toString() + ' minute ago'
        }
      }
    } else {
      if (second > 1) {
        return seconds.toString() + ' seconds ago'
      } else {
        return seconds.toString() + ' second ago'
      }
    }
  }

Thanks in advance for any help.

MMelvin0581
  • 509
  • 6
  • 20

1 Answers1

-1

timeSince=(date)=>{

  let seconds = Math.floor((new Date() - date) / 1000);

  let interval = Math.floor(seconds / 31536000);

  if (interval > 1) {
    return interval + " years";
  }
  interval = Math.floor(seconds / 2592000);
  if (interval > 1) {
    return interval + " months";
  }
  interval = Math.floor(seconds / 86400);
  if (interval > 1) {
    return interval + " days";
  }
  interval = Math.floor(seconds / 3600);
  if (interval > 1) {
    return interval + " hours";
  }
  interval = Math.floor(seconds / 60);
  if (interval > 1) {
    return interval + " minutes";
  }
  return Math.floor(seconds) + " seconds";
}
let aDay = 24*60*60*1000
console.log(timeSince(new Date(Date.now()-aDay)));
console.log(timeSince(new Date(Date.now()-aDay*2)));

It took 1 quick google search and a quick JS fiddle test sesh cause I like ES6, to find an answer. Whether it's the right one or as efficient as you're looking for is up to you. The original post --> How to format time since xxx e.g. “4 minutes ago” similar to Stack Exchange sites <-- Looking up answers is part of being a developer, and a fun one. Do your research :)

  • yeah i saw that. i know how to google. i know how to research. that implementation is basically the same as mine... – MMelvin0581 Sep 16 '18 at 19:48
  • I've been looking but finding a less wordy implementation without anything like php or an npm package has proved difficult xD. Maybe try to use template literals instead of string concatenation? It's not much but it shortens things a teensy bit. – Andrea Peekatchu Follack Sep 18 '18 at 18:07
  • yeah that could help a little. I'm definitely going to move it into a filter to hide it away a little lol. it's so ugly – MMelvin0581 Sep 19 '18 at 19:02