473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C# Equivalent to VB's Weekday function

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

Nov 18 '07 #1
9 7812
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
Nov 18 '07 #2
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.dkwrote:
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 -
Nov 18 '07 #3
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

Nov 18 '07 #4
On 2007-11-17 17:55:19 -0800, Dave <Da**********@jacobs.comsaid:
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

Nov 18 '07 #5
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
Nov 18 '07 #6
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
Nov 18 '07 #7
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" <de*****@alunharford.co.ukwrote in message
news:uD**************@TK2MSFTNGP02.phx.gbl...
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
Nov 18 '07 #8
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
Nov 18 '07 #9
That'll do the trick as well.
"Alun Harford" <de*****@alunharford.co.ukwrote in message
news:OX**************@TK2MSFTNGP02.phx.gbl...
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
Nov 18 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kd | last post by:
I am currently using a form with Weekdays Mon Tues Wed Thur Fri This is generated by a table that the days are being entered manually I would like to create a form that the days updated...
1
by: Crash | last post by:
I want to call the DateAndTime.DatePart() Method in VB from C#, i have correctly added the reference to the VB dll, etc... but when i call this function like this ...
2
by: Alan Ho | last post by:
Function Helper(ByVal obj As Object) As String Dim thisDate As Date = CDate(obj) Return WeekdayName(Weekday(thisDate, vbSunday), False, vbSunday) & "<br>" & _ thisDate.Day & "/" & thisDate.Month...
12
by: Shariq | last post by:
Could someone please help me converting the following lines of vb6 code to vb.net. The message box should display "1/3/2005" if it is executed today. Private Sub Form_Load() Dim nWeekNumber As...
2
by: jblankenburg | last post by:
Please help! I am hunting high and low for an equivalent function for MSSQL's DATENAME function. Here's the spec on the function from MSSQL's Books Online: DATENAME Returns a character...
14
by: Divit | last post by:
I want my code to tell me that (today) April 12th is the second wednesday of the month. And only thing I can find is the function to tell me what date belongs to the 2nd day in a given month and...
2
by: badboybrown | last post by:
Hello folks, I found this piece of code on this group by Dorman Blackman: > Function BusinessDays(dDate1, dDate2) As Long > BusinessDays = (DateDiff("d", dDate1, dDate2) - _ > ...
6
by: kevinjwilson | last post by:
I am trying to get the date difference between two dates but I don't want the function to include weekends in the calculation. Does anyone have an idea on how to make this work?
4
by: dgmoore | last post by:
I've hit a snag - I know this is easy, but the logic is escaping me. I need to set criteria in a query to find dates in a date field that lie between Tuesday of the current week and Tuesday of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.