Alegro
Alegro

Reputation: 7956

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 ?

Upvotes: 2

Views: 6784

Answers (5)

Kimtho6
Kimtho6

Reputation: 6184

it is as simple as this:

var culture = new CultureInfo("da-DK");
var datestring =datetime.ToString("dd MMMMM yyyy kl. hh:mm",culture)

Upvotes: 0

Mahdi Tahsildari
Mahdi Tahsildari

Reputation: 13582

DateTime    dateValue    =    new    DateTime(2008,    6,    11);      
Console.WriteLine(dateValue.ToString("ddd",    
                  new    CultureInfo("fr-FR")));

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500275

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);

Upvotes: 6

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 61952

Try dt.ToString("dddd") and then manipulate that string if necessary. See Custom Date and Time Format Strings.

Upvotes: 2

Mare Infinitus
Mare Infinitus

Reputation: 8162

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"));

Upvotes: 3

Related Questions