RBS RBS - 1 month ago 21
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
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.

Comments