Time Ago date format - Stimulus Javascript

Stimulus Js Controller:

  import { Controller } from "stimulus"

  export default class extends Controller {
    static targets = [
      "time"
    ]

    connect() {
      this.timeAgo()
    }

    timeAgo() {
      for (var index in this.timeTargets) {
        const timeTarget = this.timeTargets[index];
        if (typeof timeTarget === "object") {
          timeTarget.innerHTML = this.setTimer(timeTarget.dataset.date);
        }
      }
      // update time every minute
      setTimeout(this.timeAgo, 60000);
    }

    template(t, n) {
      return this.templates[t] && this.templates[t].replace(/%d/i, Math.abs(Math.round(n)));
    }

    setTimer(time) {
      if (!time) return;
      time = time.replace(/\.\d+/, ""); // remove milliseconds
      time = time.replace(/-/, "/").replace(/-/, "/");
      time = time.replace(/T/, " ").replace(/Z/, " UTC");
      time = time.replace(/([\+\-]\d\d)\:?(\d\d)/, " $1$2"); // -04:00 -> -0400
      time = new Date(time * 1000 || time);

      return this.templates.prefix + (
        this.seconds(time) < 45 && this.template("seconds", this.seconds(time)) ||
        this.seconds(time) < 90 && this.template("minute", 1) ||
        this.minutes(time) < 45 && this.template("minutes", this.minutes(time)) ||
        this.minutes(time) < 90 && this.template("hour", 1) ||
        this.hours(time) < 24 && this.template("hours", this.hours(time)) ||
        this.hours(time) < 42 && this.template("day", 1) ||
        this.days(time) < 30 && this.template("days", this.days(time)) ||
        this.days(time) < 45 && this.template("month", 1) ||
        this.days(time) < 365 && this.template("months", this.days(time) / 30) ||
        this.years(time) < 1.5 && this.template("year", 1) ||
        this.template("years", this.years(time))
      ) + this.templates.suffix;
    }

    get currentDate() {
      return new Date()
    }

    seconds(time) {
      return ((this.currentDate.getTime() - time) * .001) >> 0
    }

    minutes(time) {
      return this.seconds(time) / 60
    }

    hours(time) {
      return this.minutes(time) / 60
    }

    days(time) {
      return this.hours(time) / 24
    }

    years(time) {
      return this.days(time) / 365
    }

    get templates() {
      return {
        prefix: "",
        suffix: " ago",
        seconds: "Less than a minute",
        minute: "About a minute",
        minutes: "%d minutes",
        hour: "About an hour",
        hours: "About %d hours",
        day: "A day",
        days: "%d days",
        month: "About a month",
        months: "%d months",
        year: "About a year",
        years: "%d years"
      }
    }
  }

In views:

  <span data-controller="timeago" data-timeago-target="time" data-date="<%= @post.date %>"></span>