vb.net 将时间以秒为单位转换为 hh:mm:ss 格式的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8869065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:52:54  来源:igfitidea点击:

Program to convert time in seconds to hh:mm:ss format

vb.nettime-format

提问by Failed_Noob

I am trying to make a simple program to convert time given in seconds to hh:mm:ss format. But for some particular input values it produces an incorrect time format. This is what I have tried:

我正在尝试制作一个简单的程序,将时间以秒为单位转换为 hh:mm:ss 格式。但是对于某些特定的输入值,它会产生不正确的时间格式。这是我尝试过的:

Public Class Form1
    Dim Hours, Minutes, Seconds As Integer

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        lblHours.Text = "00"
        lblMinutes.Text = "00"
        lblSeconds.Text = "00"
        txtTimeSeconds.Text = ""
        txtFormattedTime.Text = ""
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate1.Click
        Seconds = Integer.Parse(txtTimeSeconds.Text)
        Hours = Seconds / 3600
        Seconds = Seconds Mod 3600
        Minutes = Seconds / 60
        Seconds = Seconds Mod 60

        lblHours.Text = Hours.ToString.PadLeft(2, "0"c)
        lblMinutes.Text = Minutes.ToString.PadLeft(2, "0"c)
        lblSeconds.Text = Seconds.ToString.PadLeft(2, "0"c)

        txtFormattedTime.Text = Hours.ToString.PadLeft(2, "0"c) & ":" & Minutes.ToString.PadLeft(2, "0"c) & ":" & Seconds.ToString.PadLeft(2, "0"c)
    End Sub
End Class

It works when the input value is 30:

它在输入值为 30 时起作用:

Enter image description here

在此处输入图片说明

It does not work when the input value is 31:

输入值为31时不起作用:

Enter image description here

在此处输入图片说明

What have I done wrong? How can I fix this problem?

我做错了什么?我该如何解决这个问题?

回答by John Woo

There is class in .NET called TimeSpanwhich makes your code easy and elegant.

.NET 中有一个名为TimeSpan 的类,它使您的代码简单而优雅。

Example:

例子:

dim iSecond as double = 0 'Total number of seconds
Dim iSpan As TimeSpan = TimeSpan.FromSeconds(iSecond)

lblHours.Text = iSpan.Hours.ToString.PadLeft(2, "0"c)
lblMinutes.Text = iSpan.Minutes.ToString.PadLeft(2, "0"c)
lblSeconds.Text = iSpan.Seconds.ToString.PadLeft(2, "0"c)

txtFormattedTime.Text = iSpan.Hours.ToString.PadLeft(2, "0"c) & ":" & _
                        iSpan.Minutes.ToString.PadLeft(2, "0"c) & ":" & _
                        iSpan.Seconds.ToString.PadLeft(2, "0"c)

回答by Hans Passant

Visual Basic has twodivision operators, /and \. The / operator produces a result that's of type Double. You calculate 31 / 60 = 0.51666... You next assign that result to an Integer, that requires rounding. Thus producing 1, not 0.

Visual Basic 有两个除法运算符,/\. / 操作符产生一个 Double 类型的结果。您计算 31 / 60 = 0.51666... 接下来将该结果分配给一个需要四舍五入的整数。因此产生1,而不是0。

You want to use the \operator, the integer division operator. It truncates the result.

您要使用\运算符,即整数除法运算符。它截断了结果。

回答by JotaPardo

I hope this code will be useful

我希望这段代码有用

Dim ts As TimeSpan = TimeSpan.FromSeconds(227) 'or --> Dim ts As New TimeSpan(0, 0, 0, 227, 0)

Dim mydate As DateTime = New DateTime(ts.Ticks)
MessageBox.Show(mydate.ToString(("HH:mm:ss")))

回答by Serge Bekenkamp

You are using integers to store your data but division gives you doubles. When converting it back to integers it gets rounded to the nearest round number. So 0.5 becomes 0 but 0.51 becomes 1.

您正在使用整数来存储您的数据,但除法会给您加倍。将其转换回整数时,它会四舍五入到最接近的整数。所以 0.5 变成 0 但 0.51 变成 1。

回答by Siva S Tenet

Dim SecondsDifference as integer = 2500  
Dim hms = TimeSpan.FromSeconds(SecondsDifference)
Dim h = hms.Hours.ToString
Dim m = hms.Minutes.ToString
Dim s = hms.Seconds.ToString
MsgBox("Hour:" + h + " Min:" + m + " Sec:" + s)

回答by Sirach Matthews

I know this one has already been answered for a while now, but I thought I might share my solution to the problem at hand. If you place the number of seconds in a TimeSpanobject, you can quite easily extract the days, hours, minutes, seconds and even fractional seconds directly using TimeSpan.toString()method. Using an identical form and object names, I used the following code to achieve the desired results.

我知道这个问题已经有一段时间了,但我想我可以分享我手头问题的解决方案。如果您将秒数放在一个TimeSpan对象中,您可以很容易地直接使用TimeSpan.toString()method提取天、小时、分钟、秒甚至小数秒。使用相同的表单和对象名称,我使用以下代码来实现所需的结果。

Public Class Form1
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
    lblHours.Text = "00"
    lblMinutes.Text = "00"
    lblSeconds.Text = "00"
    txtTimeSeconds.Text = ""
    txtFormattedTime.Text = ""
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim tsSeconds = TimeSpan.FromSeconds(Convert.ToDouble(txtTimeSeconds.Text))
    lblHours.Text = tsSeconds.ToString("hh")
    lblMinutes.Text = tsSeconds.ToString("mm")
    lblSeconds.Text = tsSeconds.ToString("ss")
    txtFormattedTime.Text = tsSeconds.ToString("hh\:mm\:ss")
End Sub
End Class

Visit herefor more information on the method used.

访问此处了解有关所用方法的更多信息。

回答by dba

For the txtformattedtime.text=...I think "ispan.tostring"would work as well.

对于txtformattedtime.text=...我认为“ispan.tostring”也可以。