Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How is CultureInfo respected when formatting dates? #36

Open
kikaragyozov opened this issue Jan 20, 2023 · 3 comments
Open

How is CultureInfo respected when formatting dates? #36

kikaragyozov opened this issue Jan 20, 2023 · 3 comments

Comments

@kikaragyozov
Copy link

kikaragyozov commented Jan 20, 2023

I fail to grasp how NumberFormat.Format(DateTime, CultureInfo) respects the provided CultureInfo object in any way or fashion. It seems to always manage to convert the date in any provided CultureInfo, even if that format does not exist for it.

That begs the question - is there an example you can provide where something unexpected is produced, or an exception of some kind is thrown due to differences in the CultureInfo object?

Perhaps the same issue persists in NumberFormat.Format(string, CultureInfo) but I think I'm wrong on this.

@andersnm
Copy link
Owner

Hi,

CultureInfo is used by the formatter to get month names, era names, date separator, decimal separator and number group separator.

Admittedly not extensively covered by tests, but at least there's one for date separators in different cultures:

@kikaragyozov
Copy link
Author

kikaragyozov commented Feb 17, 2023

Hi,

CultureInfo is used by the formatter to get month names, era names, date separator, decimal separator and number group separator.

Admittedly not extensively covered by tests, but at least there's one for date separators in different cultures:

If my goal is to simply use the date separators and what-have-you that's defined in the format we pass to NumberFormat.Format(string, CultureInfo), how could I accomplish this? Basically I wish my date to be formatted the way it is in Excel.

@andersnm
Copy link
Owner

andersnm commented Feb 17, 2023

Hi @kikaragyozov,

Keep in mind that Excel formats data differently depending on the language settings of your operating system. F.ex if somebody creates a spreadsheet in the US with dates, thousands etc, it would look differently on my machine here in Europe.

The easiest is to just pass CultureInfo.InvariantCulture as the culture, this gives you data formatted like Excel in US. If your app has localization features, you may want to use a culture based on the current user's language selection like CultureInfo.CurrentUICulture

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants