Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get DayOfWeek in a local CultureInfo?

DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
string day = dt.DayOfWeek.ToString();
day = day.Substring(0, 1).ToUpper();
MessageBox.Show(day); // result is "F"

How could I get the result in a local language (CultureInfo), for example - France and where can I find a list of languages references for this purpose ?

like image 689
Alegro Avatar asked Dec 05 '25 19:12

Alegro


2 Answers

Assuming you've already got the CultureInfo, you can use:

string dayName = dateTime.ToString("dddd", culture);

You'd then need to take the first character of it yourself - there's no custom date/time format for just that. You could use ddd for the abbreviated day name, but that's typically three characters, not one.

As for a list of cultures - you can use CultureInfo.GetCultures. It will vary by platform, and could vary over time and even by version of .NET, too.

As an aside, this code isn't ideal:

DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

If you happen to call it just before midnight at the end of a year, you could get the "old" year but then January as the month. You should evaluate DateTime.Now (or DateTime.Today) once, and then use the same value twice:

DateTime today = DateTime.Today;
DateTime startOfMonth = new DateTime(today.Year, today.Month, 1);
like image 141
Jon Skeet Avatar answered Dec 08 '25 08:12

Jon Skeet


Straight from the MSDN: MSDN on DateTime with CultureInfo

DateTime dt = DateTime.Now;
// Sets the CurrentCulture property to U.S. English.
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
// Displays dt, formatted using the ShortDatePattern
// and the CurrentThread.CurrentCulture.
Console.WriteLine(dt.ToString("d"));
like image 37
Mare Infinitus Avatar answered Dec 08 '25 09:12

Mare Infinitus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!