gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:34:54 24 4
gpt4 key购买 nike

我正在尝试制作一个简单的程序,将时间以秒为单位转换为 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

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

Enter image description here

输入值为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)

关于vb.net - 将时间以秒为单位转换为 hh :mm:ss format 的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8869065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com