Open Jeffrey-IKUU opened 1 year ago
I think I found the solution.
When exploring the dayjs folder in node_modules, I found out that the locale method is expecting three parameters. So I changed my code to
const el = dayjs().locale("nl", {}, true);
instead of
const el = dayjs().locale("nl");
and this creates a Dutch dayjs object.
When reading the docs, only the not working version (with only 1 param) is documented. This should be updated!
Inside node_modules
export function locale(preset?: string | ILocale, object?: Partial<ILocale>, isLocal?: boolean): string
Question mark suggests that the params are optional, but it just doesn't do well without all 3 params.
So, for a full Dutch dayjs instance:
dayjs().locale("nl", { weekStart: 1 }, true)
I can't reproduce this.
import dayjs from "dayjs";
import "dayjs/locale/nl";
const testTime = dayjs("2023-09-19T17:57:32.841Z");
const nl = testTime.locale("nl");
console.log({
nl,
en: testTime,
});
yields:
{
nl: M2 {
'$L': 'nl',
'$d': new Date('2023-09-19T17:57:32.000Z'),
'$y': 2023,
'$M': 8,
'$D': 19,
'$W': 2,
'$H': 19,
'$m': 57,
'$s': 32,
'$ms': 841,
'$x': {},
'$isDayjsObject': true
},
en: M2 {
'$L': 'en',
'$d': new Date('2023-09-19T17:57:32.000Z'),
'$y': 2023,
'$M': 8,
'$D': 19,
'$W': 2,
'$H': 19,
'$m': 57,
'$s': 32,
'$ms': 841,
'$x': {},
'$isDayjsObject': true
}
}
As mentioned in the docs, "Changing the global locale doesn't affect existing instances." The issue you may have been having is calling dayjs.locale("nl")
AFTER declaring your dayjs object.
Dayjs locale seems don't work on server side until I await locale file import in current scope. Try this
~/plugins/fix-dayjs.server.ts
export default defineNuxtPlugin({
name: 'fix-dayjs',
enforce: 'post',
async setup() {
await import('dayjs/locale/nl');
},
});
or
export default defineEventHandler(async (event) => {
await import('dayjs/locale/nl');
// ...
})
Describe the bug I'm trying to set the global locale to 'nl'. Every DayJS instance should have the Dutch locale, instead of the default 'en'. However, regardless of setting it, the default is still returning English:
Expected behavior I expect $L to be 'nl', not 'en'.
Information My package.json:
My code: