1
  • I am trying to display relative time in moment using the moment(...).fromNow()
  • I am using the @nuxtjs/moment module
  • It currently displays relative time as "15 days ago" etc. I want to change that to "15 d"

As per THIS answer is how it can be done currently

moment.updateLocale('en', {
    relativeTime : {
        future: "in %s",
        past:   "%s ago",
        s: function (number, withoutSuffix, key, isFuture){
            return '00:' + (number<10 ? '0':'') + number + ' minutes';
        },
        m:  "01:00 minutes",
        mm: function (number, withoutSuffix, key, isFuture){
            return (number<10 ? '0':'') + number + ':00' + ' minutes';
        },
        h:  "an hour",
        hh: "%d hours",
        d:  "a day",
        dd: "%d days",
        M:  "a month",
        MM: "%d months",
        y:  "a year",
        yy: "%d years"
    }
});

How do I apply this to the @nuxtjs/moment module

PirateApp
  • 5,433
  • 4
  • 57
  • 90

1 Answers1

1

Make a nuxt plugin and access the $moment using context

export default (context) => {
  context.$moment.updateLocale('en', {
    relativeTime: {
      future: '%s',
      past: '%s',
      s: '1 s',
      ss: '%d seconds',
      m: '1 m',
      mm: '%d m',
      h: '1 h',
      hh: '%d h',
      d: '1 d',
      dd: '%d d',
      M: '1 M',
      MM: '%d M',
      y: '1 Y',
      yy: '%d Y',
    },
  })
}
PirateApp
  • 5,433
  • 4
  • 57
  • 90