PC Review


Reply
Thread Tools Rate Thread

C# Equivalent to VB's Weekday function

 
 
Dave
Guest
Posts: n/a
 
      18th Nov 2007
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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      18th Nov 2007
Dave wrote:
> 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
 
Reply With Quote
 
Dave
Guest
Posts: n/a
 
      18th Nov 2007
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)?




On Nov 17, 7:47 pm, Arne Vajhøj <a...@vajhoej.dk> wrote:
> Dave wrote:
> > 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- Hide quoted text -
>
> - Show quoted text -


 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      18th Nov 2007
On 2007-11-17 17:55:19 -0800, Dave <(E-Mail Removed)> 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)?


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

Pete

 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      18th Nov 2007
Dave wrote:
> 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

 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      18th Nov 2007
Dave wrote:
> 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

--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
Alun Harford
Guest
Posts: n/a
 
      18th Nov 2007
Dave wrote:
> 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
 
Reply With Quote
 
Stephany Young
Guest
Posts: n/a
 
      18th Nov 2007
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);


"Alun Harford" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Dave wrote:
>> 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


 
Reply With Quote
 
Alun Harford
Guest
Posts: n/a
 
      18th Nov 2007
Stephany Young wrote:
> 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
 
Reply With Quote
 
Stephany Young
Guest
Posts: n/a
 
      18th Nov 2007
That'll do the trick as well.


"Alun Harford" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Stephany Young wrote:
>> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Weekday function Robert Crandal Microsoft Excel Programming 7 11th Jan 2010 05:48 PM
WEEKDAY() function: display TEXT not numeric weekday =?Utf-8?B?VG9t?= Microsoft Excel Misc 3 21st Nov 2006 04:32 PM
WEEKDAY Function? =?Utf-8?B?SlA=?= Microsoft Excel Worksheet Functions 5 21st Apr 2006 06:54 PM
Weekday Function John Microsoft Excel Worksheet Functions 2 7th Jan 2005 11:39 AM
weekday function Dave Microsoft Access Queries 3 17th Aug 2003 03:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:35 AM.