Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB to C# conversion DateAndTime equivalent

Tags:

c#

.net

vb.net

I'm trying to convert this VB block to C#.

  Public Function AddWorkingDays(ByVal DateIn As DateTime, _
   ByVal ShiftDate As Integer) As DateTime
    Dim b As Integer

    Dim datDate As DateTime = DateIn ' Adds the [ShiftDate] number of working days to DateIn'
    For b = 1 To ShiftDate
        datDate = datDate.AddDays(1)
        ' Loop around until we get the need non-weekend day'
        If Weekday(datDate) = 7 Then
            datDate = datDate.AddDays(2)
        End If
    Next
    Return datDate
End Function

So far I've got

public DateTime AddWorkingDays(DateTime DateIn, int ShiftDate)
{
    int b = 0;

    DateTime datDate = DateIn;
    // Adds the [ShiftDate] number of working days to DateIn
    for (b = 1; b <= ShiftDate; b++)
    {
        datDate = datDate.AddDays(1);
        // Loop around until we get the need non-weekend day
        if (DateAndTime.Weekday(datDate) == 7)
        {
            datDate = datDate.AddDays(2);
        }
    }
    return datDate;
}

I know that there doesn't exist in C# DateAndTime I just put it in the if statement to complete the block of code. My real problem is getting the IF statement to work. I am not sure if DateTime.Now.Weekday is the same statement as in the previous VB code.

like image 387
barkl3y Avatar asked Dec 11 '22 15:12

barkl3y


1 Answers

Just use the DayOfWeek enumeration, e.g.

datDate.DayOfWeek == DayOfWeek.Saturday
like image 157
George Johnston Avatar answered Dec 31 '22 04:12

George Johnston