JeliBeanMachine
JeliBeanMachine

Reputation: 123

Getting the Day of Week from a DateTimePicker

I have a 2d array and if the date selected using the dateTimePicker is a monday I want to set the row index to 1. If the date selected is a tuesday I want to set the row index to 2... I have tried do this using the code below but it doesn't seem to work:

       if (dateTimePicker.Value == DateTime.DayOfWeek.Monday)
       r = 1;  
       if (dateTimePicker.Value == DateTime.DayOfWeek.tuesday)
       r = 2;

Upvotes: 2

Views: 17222

Answers (3)

PLED
PLED

Reputation: 106

Using a Cast

int r = (int)dateTimePicker1.Value.DayOfWeek;

Upvotes: 0

Mike Cheel
Mike Cheel

Reputation: 13106

dateTimePicker.Value will contain a full DateTime. You're comparing that against a constant. Try comparing dateTimePicker.Value.DayOfWeek to the enum constants.

Upvotes: 0

Bob Kaufman
Bob Kaufman

Reputation: 12815

According to Microsoft's documentation, DateTimePicker.Value is of type DateTime.

Take the DayOfWeek property to get the value you're looking for.

Your statement should look like:

if ( dateTimePicker.Value.DayOfWeek == DayOfWeek.Monday )
 ...

Upvotes: 3

Related Questions