比较日期 - 日期格式问题

时间:2014-02-28 15:32:42

标签: asp.net sql-server visual-studio-2010 date

在我的网络表单中,我试图找出不包括周末和假期的天数。我有一个SQL数据库,其中假日日期是日期格式(yyyy-MM-dd)。用户使用格式MM / dd / yyyy添加日期。不计算我的代码的周末部分工作。弄清楚日期是否是假日,如果是,不计算它不起作用。这是我的代码。我已经尝试了所有注意到的东西。感谢任何帮助,以弄清楚如何将输入的日期与SQL日期进行比较。

Dim intCount As Integer
Dim temp As Integer

'Set same day as 1 <-- your preference. Though you can't work out averages if they are 0.
'Im not checking if this day is a holiday (though in theory you shouldn't have to!).
If StartDate = EndDate Then
    WorkingDays = 1 'Change me to your needs.
    Exit Function
End If

intCount = 0 ' Now we start counting days. 'If you always want to count the first day, set this to 1.

Do Until StartDate = EndDate

    'First, we find out if this day is a weekday or a weekend.
    'If weekday, 1 gets added to the number of days.
    Select Case Weekday(StartDate)
        Case Is = 1, 7
            intCount = intCount 'Weekend, so nothing added.
        Case Else
            intCount = intCount + 1
    End Select

    'Now, if this day was a holiday, we take it back off again!
    'Dim vsqldate = DateTime.ParseExact(StartDate, "yyyy-MM-dd", Nothing)
    'Dim vsqldate = StartDate.ToString("yyyy-MM-dd")
    'Dim vsqldate = fromDt.ToString("yyyy-MM-dd")
    Dim vsqldate = CDate(Format(StartDate, "yyyy-MM-dd"))

    Dim strWhere = "Holiday= #" & vsqldate & "#"  'change fieldname.

    If (DCount("Holiday", "tblCodesholidays", strWhere) > 0) Then 'Change to your field/table names.
        intCount = intCount - 1
    End If

    StartDate = StartDate.AddDays(1) 'We move to the next day.

Loop

WorkingDays = intCount

1 个答案:

答案 0 :(得分:0)

从SQL Server的角度来看,格式为“yyyymmdd”的日期被视为语言中立。所以,在你的VB.Net代码中试试这个:

Dim vsqldate As DateTime 
vsqldate = DateTime.ParseExact(StartDate.ToString("MM/dd/yyyy"))
Dim strWhere = "Holiday= '" & vsqldate.ToString("yyyyMMdd") & "'"  'change fieldname.