1

I've got this code:

Calendar cal = CultureInfo.InvariantCulture.Calendar;
String dow = cal.GetDayOfWeek(DateTime.Now).ToString();
if (dow.Equals("Monday"))

...but I wonder if it would still work in German-speaking locales ("Montag") or Spanish-speaking locales ("Lunes"). If not - if looking for "Monday" is problematic - how can I get an int that reprsents day of the week. And even in that case, is 0 always Sunday, or is it sometimes Monday (or even something else)?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

4

It's already an enum value, so don't cast it to a string before doing the comparison:

Calendar cal = CultureInfo.InvariantCulture.Calendar;
DayOfWeek dow = cal.GetDayOfWeek(DateTime.Now);

if (dow == DayOfWeek.Monday)
{

}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • So "DayOfWeek.Monday" is okay? I don't have to worry about DayOfWeek.Montag or DayOfWeek.Lunes or such? – B. Clay Shannon-B. Crow Raven Nov 19 '14 at 01:28
  • 2
    @B.ClayShannon An enum is a numeric value, not a string - so you don't have to worry about culture when you do the comparison. – slugster Nov 19 '14 at 01:30
  • 2
    APIs themselves don't get localized in WinRT, so it will always be DayOfWeek.Monday with that semantic, regardless of the current culture setting of the OS. Ultimately, all such details are compiled away and the code is just comparing numbers. – Kraig Brockschmidt - MSFT Nov 19 '14 at 06:39