C# Equivalent to VB's Weekday function

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

What is the C# Equivalent to VB's Weekday function? I'm trying to
convert this:

Select Case Weekday(CurrentDate)
Case 1 ' Sunday
WKG = CurrentDate
Case 2 ' Monday
WKG = CurrentDate + 6
Case 3 ' Tuesday
WKG = CurrentDate + 5
Case 4 ' Wednesday
WKG = CurrentDate + 4
Case 5 ' Thursday
WKG = CurrentDate + 3
Case 6 ' Friday
WKG = CurrentDate + 2
Case 7 ' Saturday
WKG = CurrentDate + 1
End Select

If you know an easier way to do this...I'm open.

Thanks
 
Dave said:
What is the C# Equivalent to VB's Weekday function? I'm trying to
convert this:

Select Case Weekday(CurrentDate)
Case 1 ' Sunday
WKG = CurrentDate
Case 2 ' Monday
WKG = CurrentDate + 6
Case 3 ' Tuesday
WKG = CurrentDate + 5
Case 4 ' Wednesday
WKG = CurrentDate + 4
Case 5 ' Thursday
WKG = CurrentDate + 3
Case 6 ' Friday
WKG = CurrentDate + 2
Case 7 ' Saturday
WKG = CurrentDate + 1
End Select

If you know an easier way to do this...I'm open.

DateTime.Now.DayOfWeek

Arne
 
Wow...I think that was the fastest response I've ever gotten!

And if CurrentDate on the above Function was represented by
Calendar1.SelectedDate? Like Weekday(Calendar1.SelectedDate)?
 
Wow...I think that was the fastest response I've ever gotten!

And if CurrentDate on the above Function was represented by
Calendar1.SelectedDate? Like Weekday(Calendar1.SelectedDate)?

DayOfWeek is a property of the DateTime class. You can get it from any
DateTime instance, not just the static DateTime.Now instance.

Pete
 
Dave said:
Wow...I think that was the fastest response I've ever gotten!

And if CurrentDate on the above Function was represented by
Calendar1.SelectedDate? Like Weekday(Calendar1.SelectedDate)?

Calendar1.SelectedDate.DayOfWeek

Arne
 
Dave said:
Wow...I think that was the fastest response I've ever gotten!

And if CurrentDate on the above Function was represented by
Calendar1.SelectedDate? Like Weekday(Calendar1.SelectedDate)?

Calendar1.SelectedDate.DayOfWeek
 
Dave said:
What is the C# Equivalent to VB's Weekday function? I'm trying to
convert this:

Select Case Weekday(CurrentDate)
Case 1 ' Sunday
WKG = CurrentDate
Case 2 ' Monday
WKG = CurrentDate + 6
Case 3 ' Tuesday
WKG = CurrentDate + 5
Case 4 ' Wednesday
WKG = CurrentDate + 4
Case 5 ' Thursday
WKG = CurrentDate + 3
Case 6 ' Friday
WKG = CurrentDate + 2
Case 7 ' Saturday
WKG = CurrentDate + 1
End Select

If you know an easier way to do this...I'm open.

Much easier. Because of the way the DayOfWeek enum is defined, you can
replace the whole switch statement with:

WKG = CurrentDate.AddDays((int)CurrentDate.DayOfWeek);

Alun Harford
 
If you do that, Monday becomes Tuesday, Tuesday becomes Thursday, Wednesday
becomes Saturday etc.

What he is looking for is the date of the NEXT Sunday if 'Today' is NOT a
Sunday.

That would be something like:

if (CurrentDate.DayOfWeek == DayOfWeek.Sunday)
WKG = CurrentDate;
else
WKG = CurrentDate.AddDays(7 - (int)CurrentDate.DayOfWeek);
 
Stephany said:
If you do that, Monday becomes Tuesday, Tuesday becomes Thursday,
Wednesday becomes Saturday etc.

What he is looking for is the date of the NEXT Sunday if 'Today' is NOT
a Sunday.

That would be something like:

if (CurrentDate.DayOfWeek == DayOfWeek.Sunday)
WKG = CurrentDate;
else
WKG = CurrentDate.AddDays(7 - (int)CurrentDate.DayOfWeek);

Eugh. Sorry - I claim it's late here :-)

How about:

WKG = CurrentDate.AddDays((7-(int)CurrentDate.DayOfWeek)%7);

Alun Harford
 
That'll do the trick as well.


Alun Harford said:
Eugh. Sorry - I claim it's late here :-)

How about:

WKG = CurrentDate.AddDays((7-(int)CurrentDate.DayOfWeek)%7);

Alun Harford
 
Back
Top