0

I'm using the below script to custom my date, sometimes when the time is less than minute it works funky like that.

How can i custom it to say 1 min ago if the date is less than a minute? or simply remove the seconds ago functionality?

-9236 seconds ago

function dateFormatter(date) {

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

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

    if (interval > 1) {
        return interval + " Years Ago ";
    }
    interval = Math.floor(seconds / 2592000);
    if (interval > 1) {
        return interval + " Months Ago ";
    }
    interval = Math.floor(seconds / 86400);
    if (interval > 1) {
        return interval + " Days Ago ";
    }
    interval = Math.floor(seconds / 3600);
    if (interval > 1) {
        return interval + " Hrs ago ";
    }
    interval = Math.floor(seconds / 60);
    if (interval > 1) {
        return interval + " mins ago ";
    }
    return Math.floor(seconds) + " seconds ago";
}
Amir
  • 527
  • 1
  • 6
  • 23

0 Answers0