我是一个SQL人员,但我需要一个函数来计算VB.NET中两个日期之间的工作日数。我不需要担心假期。不幸的是,我的尝试是徒劳的。非常感谢
这将包含在Reporting Service 2008 R2中的自定义代码中。
答案 0 :(得分:7)
试试这个。我修改了我一直在使用的现有功能。这将在SSRS 2008中有效。注意,您也可以使用C#编写代码,如果您对此更加熟悉,并且在部署之后,只需从报告中引用程序集。1
public Shared Function Weekdays(ByRef startDate As Date, ByRef endDate As Date ) As integer
dim numWeekdays as Integer
dim totalDays as Integer
dim WeekendDays as Integer
numWeekdays = 0
WeekendDays = 0
totalDays = DateDiff(DateInterval.Day, startDate , endDate ) + 1
for i as integer = 1 to totalDays
if DatePart(dateinterval.weekday,startDate) = 1 then
WeekendDays = WeekendDays + 1
end if
if DatePart(dateinterval.weekday, startDate) = 7 then
WeekendDays = WeekendDays + 1
end if
startDate = DateAdd("d", 1, startDate)
next
numWeekdays = totalDays - WeekendDays
return numWeekdays
End Function
答案 1 :(得分:5)
您无需检查这些日期之间的每一天是否为工作日。
如果有n
天,则有int(n / 7)
个完整周,每个星期有5个工作日,因此工作日为5 * int(n / 7)
。
然后,您需要检查剩余部分周(0..6天)的日期。
答案 2 :(得分:1)
'这是我的版本vb.net 2013及其作品
Private Sub enddate_ValueChanged(sender As Object, e As EventArgs) Handles enddate.ValueChanged
Dim countsun As Integer = 0
Dim countsat As Integer = 0
Dim nonholiday As Integer = 0
Dim totalDays = (enddate.Value - startdate.Value).Days
For i = 0 To totalDays
Dim Weekday As DayOfWeek = startdate.Value.Date.AddDays(i).DayOfWeek
If Weekday = DayOfWeek.Saturday Then
countsat += 1
End If
If Weekday = DayOfWeek.Sunday Then
countsun += 1
End If
If Weekday <> DayOfWeek.Saturday AndAlso Weekday <> DayOfWeek.Sunday Then
nonholiday += 1
End If
Next
lblSumHeadCount.Text = nonholiday & " Day(s)"
End Sub
&#39;感谢Gabe
答案 3 :(得分:0)
这可能有助于其他人寻找此事。此功能不会日复一日地循环。
public static double WorkDays(DateTime start, DateTime end)
{
var delta = end.AddDays(1) - start;
var fullWeeks = (int)(delta.TotalDays / 7);
var workDays = fullWeeks * 5;
var partialWeekDays = delta.TotalDays % 7;
if (partialWeekDays > 0)
{
var startWeekday = start.DayOfWeek;
var endWeekday = end.DayOfWeek;
if (startWeekday == DayOfWeek.Sunday || startWeekday == DayOfWeek.Saturday)
partialWeekDays--;
if (startWeekday > DayOfWeek.Sunday && startWeekday < DayOfWeek.Saturday &&
startWeekday > endWeekday)
partialWeekDays--;
if ((endWeekday == DayOfWeek.Sunday || endWeekday == DayOfWeek.Saturday) && endWeekday != startWeekday)
partialWeekDays--;
if (endWeekday > DayOfWeek.Sunday && endWeekday < DayOfWeek.Saturday &&
startWeekday > endWeekday)
partialWeekDays--;
}
return workDays + partialWeekDays;
}
答案 4 :(得分:0)
我有一个表达式,用于计算两个日期之间特定星期几的数量。因此,如果您在周一,周二......星期五添加金额,您将获得工作日的数量。
=SUM(int((DatePart("d",Fields!dteEndDateTime.Value) - Weekday(DateAdd("d",1-**[DayofWeek]**,Fields!dteEndDateTime.Value)) - DatePart("d",Fields!dteStartDateTime.Value) + 8)/7))
[DayofWeek] 是表示一天的整数:1 - Sunday; 2 - Monday
因此,计算dteStartDateTime
和dteEndDateTime
之间星期一的数量的表达式为:
=SUM(int((DatePart("d",Fields!dteEndDateTime.Value) - Weekday(DateAdd("d",1-2,Fields!dteEndDateTime.Value)) - DatePart("d",Fields!dteStartDateTime.Value) + 8)/7))
答案 5 :(得分:0)
我认为这可能会有所帮助
Public Shared Function WeekEndsBetweenDates(ByVal StartDate As Date, ByVal EndDate As Date) As Integer
Dim wkday, wkend As Integer
For i As Integer = 0 To DateDiff(DateInterval.Day, StartDate, EndDate)
If DateAdd(DateInterval.Day, i, StartDate).DayOfWeek = DayOfWeek.Saturday Or DateAdd(DateInterval.Day, i, StartDate).DayOfWeek = DayOfWeek.Sunday Then
wkend += 1
Else
wkday += 1
End If
Next
Return wkend
End Function
在这里,我返回了“wkend”,它是函数中变量的周末部分。或者,您可以将返回值更改为“wkday”,这是所选日期之间的工作日数。 我希望这有帮助。它虽然适用于我