计算2个日期,不包括周末VB net

时间:2016-03-25 06:38:41

标签: vb.net date vb.net-2010 weekend

我正在尝试计算截止日期到期日期。如果日期返回在一天内通过截止日期。会有罚款。

这是我的计算代码

Dim st As Integer = MetroDateTime1.Value.Date.Subtract(Label9.Text).Days

If (Label6.Text) > (MetroDateTime1.Value.Date) Then _
  MessageBox.Show("Date return must not below to date borrowed", _
  "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

  MetroDateTime1.Focus()

ElseIf (st > 0) Then
  MetroTextBox7.Text = (st * 5).ToString()
Else
  MetroTextBox7.Text = 0
End If

' Metrotextbox7是罚款的文本框。但是,如何计算不包括周末的日期?

1 个答案:

答案 0 :(得分:1)

'获得营业日

   Public Shared Function GetBusinessDays(startDay As DateTime, endDay As DateTime) As Integer

    Dim today = Date.Today
    Dim weekend = {DayOfWeek.Saturday, DayOfWeek.Sunday}
    Dim businessDays = 
    From d In Enumerable.Range(0, (endDay.Date - startDay.Date).Days + 1)
    Select day = today.AddDays(d)
    Where Not weekend.Contains(day.DayOfWeek)

    Return businessDays.Count()

 End Function

already answered question