English 中文(简体)
将时间改为h:毫米:格式
原标题:Program to convert time in seconds to hh:mm:ss format

我试图制定简单的方案,将第二代时间转换为h:mm:s Format。 但对某些特定投入价值而言,它会产生一种不正确的时间格式。 这是我尝试的:

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

在投入价值为30时,它就发挥了作用:

“Enter像在此描述”</p

在投入价值为31时,它并不发挥作用:

Enter image description here

我做了哪些错误? 我如何解决这一问题?

最佳回答

NET中有一个称为timeSpan,使你的代码变得容易和容易。

例:

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)
问题回答

视觉基础2 司操作员,/。 经营人产生一种双重结果。 页: 1 接下来,你将这一结果交给了需要四舍五入的Integer。 因此,生产1,而不是0。

您希望使用<代码>的操作者,即编造者。 由此得出结果。

我希望这一守则将是有益的。

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")))

你们正在利用分类来储存你的数据,但分工给你两倍。 在将它重新转化为愤怒时,它被四舍五入到最近的四舍五入。 因此,0.5个变成0个,0.51个改为1个。

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)

<>txtformattedtime.text=...... 我认为ispan.tostring”也将发挥作用。

我知道,现在人们已经回答了这个问题,但我认为我可能赞同我对当前问题的解决办法。 如果你将二字数放在<代码>Span的物体上,你可以非常容易地直接使用<代码>Span.toString()的天数、小时、分钟、秒、秒、秒钟甚至分秒。 我使用相同的表格和标名,使用以下代码取得预期成果。

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

访问,以获得更多关于所用方法的信息。





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签