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

Chapter 08. DateTime and culture specific formatting #87

Open
Dreamoochy opened this issue Jun 27, 2022 · 3 comments
Open

Chapter 08. DateTime and culture specific formatting #87

Dreamoochy opened this issue Jun 27, 2022 · 3 comments

Comments

@Dreamoochy
Copy link

I have French interface language in my OS (along with English), as well as french regional formats.
When I run "Christmas" example, i get

Christmas: dimanche, 25 decembre 2022
Christmas is in month 12 of the year.
Christmas is day 359 of the year.
Christmas 2022 is on a Sunday.

Note that the first line shows weekday and month in French, while the last one - in English.
So default formatting is culture-specific, while the one with christmas.DayOfYear appears to be culture-invariant. But why does the program use different cultures without any intentional switching made by a programmer?

@markjprice
Copy link
Owner

markjprice commented Jun 27, 2022

I think you mean DayOfWeek. DayOfYear is a number between 1 and 366. DateTime.DayOfWeek returns an enum and they cannot be localized so they are in effect always culture-invariant. You would need to use a format code like the first example instead.

@markjprice
Copy link
Owner

I will add a paragraph about this in the next edition because it is an interesting example that I'm sure lots of people are confused by.

@markjprice
Copy link
Owner

You could also use the following to localize the enum value:

WriteLine("Christmas {0} is on a {1}.",
  arg0: christmas.Year,
  arg1: DateTimeFormatInfo.CurrentInfo.GetDayName(christmas.DayOfWeek));

And to get the abbreviated name:

DateTimeFormatInfo.CurrentInfo.GetAbbreviatedDayName(christmas.DayOfWeek)

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