moment / luxon

⏱ A library for working with dates and times in JS
https://moment.github.io/luxon
MIT License
15.05k stars 728 forks source link

Question: Global settings default locale, will it override the settings for simultaneous requests? #1566

Closed stonetwig closed 5 months ago

stonetwig commented 6 months ago

Hello dear Luxon authors.

First and foremost, thanks for such a great library. You are doing great work.

I have a question regarding the defaultLocale.

On your website and api documentation you state: Set the default locale to create DateTimes with. _Does not affect existing instances_.

I am a bit confused on the "does not affect existing instances" statement here. I have a web app where I change the default locale on each request like this:

import { Settings, DateTime } from 'luxon';

function init(defaultLocale = 'en') {
  Settings.defaultLocale = defaultLocale;
  Settings.defaultZone = 'Europe/Stockholm';
}

export { init, DateTime };

I then use the exported variables here and call the init function on each request start with the user specific locale. On simultaneous requests, will this overwrite the defaultLocale setting or does this count as seperate existing instances? I never import the DateTime directly but I also do not create a new instance of it afaik.

Thanks in advance, stonetwig

diesieben07 commented 6 months ago

Instance here refers to the actual JavaScript object instance. What this means is that setting defaultLocale will not affect any DateTime instances that you have already created previously. For example:


Settings.defaultLocale = 'de';
const germanDate = DateTime.local();
Settings.defaultLocale = 'en';
const englishDate = DateTime.local();

console.log(germanDate.locale); // still 'de'
console.log(englishDate.locale); // still 'en'