Equivalent of WeekDay Function of VB6 in C#

RBS picture RBS · Dec 30, 2008 · Viewed 13k times · Source

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

LeppyR64 picture LeppyR64 · Dec 30, 2008
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

as Tuesday is 4 days after Friday.