1

is there any function of datetime return dayofweekindex? such as:
int Todaywhat_is_Index= = DateTime.Now.IndexOfDayofThisWeek;
if Today is friday, it must be return 5
ifToday is Saturday, it must be return 6
ifToday is Sunday, it must be return 0

Penguen
  • 16,836
  • 42
  • 130
  • 205

3 Answers3

17

This little one-liner works independent of locale, with always Friday == 5

int x = (int)System.Globalization.CultureInfo
        .InvariantCulture.Calendar.GetDayOfWeek(DateTime.Now);
H H
  • 263,252
  • 30
  • 330
  • 514
0

Yes, check DateTime.DayOfWeek.

Please note this depends on the regional settings (here in Europe Monday is the first day of the week).

Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
  • 2
    Be warned: this value varies by culture. Depending on where you are day 0 may be either Sunday or Monday. – Joel Coehoorn Feb 27 '09 at 14:38
  • 1
    Don't forget that you need to cast it back to an int to get it as a number. – Powerlord Feb 27 '09 at 14:39
  • Use DateTimeFormatInfo.InvariantInfo.DayNames to avoid culture – Greg Feb 27 '09 at 14:56
  • 2
    @GerrieSchenck I know your comment was made some years ago so perhaps things have changed but the current doc states: If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday). No mention of any regional/culture variances. – Wayne Phipps Feb 10 '13 at 13:12
-1

Try this:

 int dayOfWeek;
 dayOfWeek = (int)System.DateTime.Now.DayOfWeek - (int)System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek + 1
Storm
  • 684
  • 9
  • 20