【问题标题】:How to get total number of hours in a year using vb.net?如何使用 vb.net 获得一年中的总小时数?
【发布时间】:2013-01-16 09:51:43
【问题描述】:

我想使用 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

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    你可以用这个:

    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
    

    【讨论】:

    • TotalHours 是双倍的。我们不需要把它转换成整数吗??
    • @Palash 完全正确。已编辑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    相关资源
    最近更新 更多