RBS RBS - 1 year ago 160
C# Question

Equivalent of WeekDay Function of 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 any one help?

Answer Source
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:


as Tuesday is 4 days after Friday.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download