【发布时间】:2014-04-01 16:47:59
【问题描述】:
在我的网络表单中,我试图计算不包括周末和节假日的休假天数。我有一个 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
【问题讨论】:
标签: asp.net sql-server visual-studio-2010 date