VB转C#转换DateAndTime等价

时间:2013-04-18 16:08:03

标签: c# .net vb.net

我正在尝试将此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代码中的语句相同。

2 个答案:

答案 0 :(得分:6)

只需使用DayOfWeek枚举,例如

datDate.DayOfWeek == DayOfWeek.Saturday

答案 1 :(得分:5)

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

    if (datDate.DayOfWeek == DayOfWeek.Saturday)
    {
        datDate = datDate.AddDays(2);
    }