ExcelNumberFormat icon indicating copy to clipboard operation
ExcelNumberFormat copied to clipboard

How is CultureInfo respected when formatting dates?

Open kikaragyozov opened this issue 2 years ago • 3 comments

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.

kikaragyozov avatar Jan 20 '23 14:01 kikaragyozov

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:

https://github.com/andersnm/ExcelNumberFormat/blob/38c6a71919ad3895e3ba61d7a888012670d26303/test/ExcelNumberFormat.Tests/Class1.cs#L495

andersnm avatar Jan 24 '23 06:01 andersnm

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:

https://github.com/andersnm/ExcelNumberFormat/blob/38c6a71919ad3895e3ba61d7a888012670d26303/test/ExcelNumberFormat.Tests/Class1.cs#L495

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.

kikaragyozov avatar Feb 17 '23 09:02 kikaragyozov

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

andersnm avatar Feb 17 '23 10:02 andersnm