Dayjs: Plugins: how extend only one instance of dayjs?

Created on 11 Aug 2019  路  7Comments  路  Source: iamkun/dayjs

I'm evaluating this library for react-day-picker and I have a concern about plugins.

For what I understand, extending dayjs with plugins will affect all the instances of dayjs.

E.g. if I extend dayjs with weekOfYear plugin, the dayjs instances used by an application implementing react-day-picker+dayjs will be extended too. Is it correct? If yes, how to extend just one instance of dayjs?

Most helpful comment

Thanks @iamkun for your answer,

I don't agree much with this design choice. Not that I expect it giving practical problems for now, but an external component using dayjs would alter how dayjs works in the whole app. This doesn't sound correct to me. What I would have preferred is a factory like:

const dayjs = dayjs.factory();
dayjs.extend(aPlugin); // only this instance of dayjs will use `aPlugin`

Just my feedback! Thanks for your work on dayjs!

All 7 comments

Yes, and I can鈥檛 see if there is a problem. What鈥檚 your concern, please?

Thanks @iamkun for your answer,

I don't agree much with this design choice. Not that I expect it giving practical problems for now, but an external component using dayjs would alter how dayjs works in the whole app. This doesn't sound correct to me. What I would have preferred is a factory like:

const dayjs = dayjs.factory();
dayjs.extend(aPlugin); // only this instance of dayjs will use `aPlugin`

Just my feedback! Thanks for your work on dayjs!

Thanks. To keep only one type of dayjs might be better I think. 馃槉

I'll close this issue since it's been a while since it's been opened. Feel free to reopen if you have updates on this

@iamkun, I found this issue as I was actually looking for the sort of factory function.

The problem I have is this:

dayjs.extend(updateLocale);

dayjs.updateLocale('en', {
  relativeTime: {
    future: 'in %s',
    past: '%s ago',
    s: '<1 min',
    m: '1 min',
    mm: '%d mins',
    h: '1 hour',
    hh: '%d hours',
  },
});

So I updated locales according to our product needs, but now it will update it for the entire app which I believe is not what we want.

What can be a work around here?

@szympajka there's no dayjs factory function atm.

In your case, it's better to use two locales.

e.g.

import somelocale from 'dayjs/locale/somelocale'

// update somelocale to other config and name
const otherlocale = {
  name: 'otherlocale',
   ...somelocale
}

// then register otherlocale
dayjs().locale(otherlocale)

// then you will have two locale config that you want

dayjs().locale(somelocale)
dayjs().locale(otherlocale)

thanks, @iamkun, it helped :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oliv9286 picture oliv9286  路  4Comments

g1eny0ung picture g1eny0ung  路  4Comments

paraself picture paraself  路  3Comments

Newbie012 picture Newbie012  路  4Comments

idasbiste picture idasbiste  路  4Comments