nuxt-community / dayjs-module

Day.js module for your Nuxt 2 project.
https://www.npmjs.com/package/@nuxtjs/dayjs
MIT License
261 stars 16 forks source link

Question: How to use updateLocale in nuxtjs/dayjs? #347

Open GabrielOkane opened 3 years ago

GabrielOkane commented 3 years ago

I would like to change the format for relativeTime: https://day.js.org/docs/en/customization/relative-time. But i dont know how implement in nuxtconfig

k4sud0n commented 3 years ago

Make a dayjs.js file in plugins folder and write code below.

export default function ({ $dayjs }) {
  $dayjs.updateLocale('en', {
    relativeTime: {
      future: 'in %s',
      past: '%s ago',
      s: 'a few seconds',
      m: 'a minute',
      mm: '%d 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',
    },
  })
}

And add this plugin to nuxt.config.js.

// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
plugins: [ '~/plugins/dayjs'],

Finally, add updateLocale to dayjs config in nuxt.config.js

...

dayjs: {
    locales: ['en'],
    defaultLocale: 'en',
    defaultTimeZone: 'America/New_York',
    plugins: ['timezone', 'relativeTime', 'updateLocale'],
  },

...
cdwmhcc commented 3 years ago

@k4sud0n How to use in nuxt3?