Penguen
Penguen

Reputation: 17268

Return index value datetime.now.dayofweek but how?

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

Upvotes: 1

Views: 13035

Answers (3)

Ziv Bar-Lev
Ziv Bar-Lev

Reputation: 1

Try this:

 int dayOfWeek;
 dayOfWeek = (int)System.DateTime.Now.DayOfWeek - (int)System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek + 1

Upvotes: -1

Henk Holterman
Henk Holterman

Reputation: 273179

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

int x = (int)System.Globalization.CultureInfo
        .InvariantCulture.Calendar.GetDayOfWeek(DateTime.Now);

Upvotes: 17

Gerrie Schenck
Gerrie Schenck

Reputation: 22368

Yes, check DateTime.DayOfWeek.

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

Upvotes: 0

Related Questions