fumeapp / dayjs

Nuxt V3 module for Day.js
Apache License 2.0
111 stars 12 forks source link

relativeTime thresholds options? #5

Closed Extarys closed 1 year ago

Extarys commented 1 year ago

I need to update thresholds for the relativeTime plugin, and update updateLocale accordingly.

https://day.js.org/docs/en/customization/relative-time

Is it possible with this plugin / is it hard to implement?

acidjazz commented 1 year ago

no problem, i'll have this in shortly

Extarys commented 1 year ago

Thank you, Sensei!

acidjazz commented 1 year ago

grab v1.1.2 and you should be all set https://github.com/fumeapp/dayjs#optional-relativetime-customization

Extarys commented 1 year ago

Wow, thanks @acidjazz ! I don't mean to offend you and the free work you're doing, but is it planned to also add thresholds eventually? :)

acidjazz commented 1 year ago

is that this? https://day.js.org/docs/en/customization/relative-time#relative-time-thresholds-and-rounding

Extarys commented 1 year ago

Yes this is it :P

rafaelragib commented 1 year ago

Hey @acidjazz! Thanks for the awesome work. Could you give an update about the integration of thresholds? I am not able to add it using the updateLocale Thanks!

GaborTorma commented 1 year ago

Hey @acidjazz! Thanks for the awesome work. Could you give an update about the integration of thresholds? I am not able to add it using the updateLocale Thanks!

You can extend with configuration via nuxt plugin like this: More Extending If you need automatic i18n support, you can use my nuxt-dayjs-i18n module.

rafaelragib commented 1 year ago
 dayjs: {
      locales: ['en'],
      plugins: ['relativeTime', 'utc', 'timezone'],
      defaultLocale: [ 'en', {
        thresholds: [
          { l: 's', r: 1 },
          { l: 'm', r: 1 },
          { l: 'mm', r: 59, d: 'minute' },
          { l: 'h', r: 1 },
          { l: 'hh', r: 23, d: 'hour' },
          { l: 'd', r: 1 },
          { l: 'dd', r: 29, d: 'day' },
          { l: 'M', r: 1 },
          { l: 'MM', r: 11, d: 'month' },
          { l: 'y' },
          { l: 'yy', d: 'year' }
        ]
      }]
    },

Not working for my side. Not sure what I am doing wrong here. (Changing the 45 minutes to 59 minutes)

GaborTorma commented 1 year ago

@rafaelragib: I think defaultLocale has no thresholds option. You need to pass it to relativeTime options in extend.

// plugins/relative-time.plugin.ts
import relativeTime from 'dayjs/plugins/relativetime'  // I'm not sure the import statement.

export default defineNuxtPlugin(() => {
  useDayjs().extend(relativeTime, {
    thresholds: [
      { l: 's', r: 1 },
      { l: 'm', r: 1 },
      { l: 'mm', r: 59, d: 'minute' },
      { l: 'h', r: 1 },
      { l: 'hh', r: 23, d: 'hour' },
      { l: 'd', r: 1 },
      { l: 'dd', r: 29, d: 'day' },
      { l: 'M', r: 1 },
      { l: 'MM', r: 11, d: 'month' },
      { l: 'y' },
      { l: 'yy', d: 'year' },
    ],
  })
})
rafaelragib commented 1 year ago

Thanks @GaborTorma ! I have added the changes you have suggested

import relativeTime from 'dayjs/plugin/relativeTime';

export default defineNuxtPlugin(() => {
  useDayjs().extend(relativeTime, {
    thresholds: [
      { l: 's', r: 1 },
      { l: 'm', r: 1 },
      { l: 'mm', r: 59, d: 'minute' },
      { l: 'h', r: 1 },
      { l: 'hh', r: 23, d: 'hour' },
      { l: 'd', r: 1 },
      { l: 'dd', r: 29, d: 'day' },
      { l: 'M', r: 1 },
      { l: 'MM', r: 11, d: 'month' },
      { l: 'y' },
      { l: 'yy', d: 'year' }
    ]
  });
});

The thresholds is still not being set. It is getting the default 45 minutes still.

const publishTimeFromNow = computed(() => {
  return dayjs(publication_time_utc).utc().fromNow();
}); 
demyan1411 commented 3 months ago

Not works for me =(

mkody commented 3 months ago

Suggested changes using useDayjs().extend() in plugins/ doesn't work. This issue should be reopened, only half of it is done.