使用VB.Net获取一年中的总小时数

时间:2013-01-16 09:51:43

标签: vb.net

我希望使用VB.Net获取一年中的总小时数。是否有任何方法或功能可以帮助我们实现这一目标?

我希望得到类似的内容:

Private Function GetTotalHours(ByVal year As String) As String
    Dim time As String = String.Empty
    Try
         ' Calculate here...

    Catch ex As Exception

    End Try        
    Return time
End Function

并使用它:

TextBox1.Text = GetTotalHours('2008')   ' Result will be = 8784
TextBox1.Text = GetTotalHours('2013')   ' Result will be = 8760

1 个答案:

答案 0 :(得分:5)

你可以用这个:

Public Function GetTotalHours(ByVal year As Integer) As Integer
    Dim dtAux As New Date(year, 1, 1)
    Dim ts As TimeSpan = dtAux.AddYears(1) - dtAux

    Return CInt(ts.TotalHours)
End Function