4

我正在尝试将此 VB 块转换为 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

到目前为止我有

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;
}

我知道 C# DateAndTime 中不存在我只是将它放在 if 语句中以完成代码块。我真正的问题是让 IF 语句起作用。我不确定 DateTime.Now.Weekday 是否与之前的 VB 代码中的语句相同。

4

2 回答 2

7

只需使用DayOfWeek枚举,例如

datDate.DayOfWeek == DayOfWeek.Saturday
于 2013-04-18T16:10:12.953 回答
6

Weekday返回7for Saturday,因此您可以使用DateTime.DayOfWeek属性并将其与 进行比较DayOfWeek.Saturday

    if (datDate.DayOfWeek == DayOfWeek.Saturday)
    {
        datDate = datDate.AddDays(2);
    }
于 2013-04-18T16:11:00.437 回答