Equivalent to WeekDay function for VB6 in C #

In VB6 code, I have the following:

dim I as Long I = Weekday(Now, vbFriday) 

I want the equivalent in C #. Can anyone help?

+6
c # datetime vb6 dayofweek weekday
source share
4 answers
 public static int Weekday(DateTime dt, DayOfWeek startOfWeek) { return (dt.DayOfWeek - startOfWeek + 7) % 7; } 

This can be called using:

 DateTime dt = DateTime.Now; Console.WriteLine(Weekday(dt, DayOfWeek.Friday)); 

The above outputs:

 4 

like tuesday 4 days after friday.

+12
source share

Do you mean the DateTime.DayOfWeek property?

 DayOfWeek dow = DateTime.Now.DayOfWeek; 
+3
source share

Yes, every DateTime value has a built-in DayOfWeek property that returns an enumeration with the same name ...

 DayOfWeek dow = DateTime.Now.DayOfWeek; 

If you want the integral value to just pass the enumeration value to int.

 int dow = (int)(DateTime.Now.DayOfWeek); 

You will need to add a constant from 1 to 6 and make Mod 7 in order to rebuild it on a different day except Sunday, however ...

+2
source share

I do not think there is an equivalent of two arguments to the form of the VB Weekday function.

You can imitate this using something like this:

 private static int Weekday(DateTime date, DayOfWeek startDay) { int diff; DayOfWeek dow = date.DayOfWeek; diff = dow - startDay; if (diff < 0) { diff += 7; } return diff; } 

Then let's call it like this:

 int i = Weekday(DateTime.Now, DayOfWeek.Friday); 

He returns 4 for today, since Tuesday is 4 days after Friday.

+1
source share

All Articles