格式化时间 (HH:MM:SS)
我从小时、分钟和秒的数字上升和下降中获取值。
问题是,例如,如果时间是上午 9.15,它将显示为 9:15:0
我想要做的是格式化它们,以便如果任何值(小时、分钟或秒)小于 10 ,它会在数字前添加 0,因此数字显示为 09:15:00。
我尝试的是这个,但它不起作用:
Sub BtnSetClick(sender As Object, e As EventArgs)
lbl8.Visible = True
Dim nmTime As String = nmHour.Value.ToString + nmMin.Value.ToString + nmSec.Value.ToString
lblST.Text.Format(nmTime.ToString, "HH:MM:SS")
lblST.Text = (nmTime)
lblST.Visible = True
End Sub
I'm taking values from Numeric Ups and Downs for Hours, Minutes and Seconds.
The problem is that, for example, if the time for example is 9.15 am it will show as 9:15:0
What I want to do is format them so if any of the values(Hours, Minutes or seconds) is less than 10, it will add a 0 before the number so as the number shows as 09:15:00.
What I tried is this but it does not work:
Sub BtnSetClick(sender As Object, e As EventArgs)
lbl8.Visible = True
Dim nmTime As String = nmHour.Value.ToString + nmMin.Value.ToString + nmSec.Value.ToString
lblST.Text.Format(nmTime.ToString, "HH:MM:SS")
lblST.Text = (nmTime)
lblST.Visible = True
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您似乎通过多次将所有内容转换为字符串来做到这一点,请尝试如下操作:
TimeSpan.ToString 很有用。
编辑:更新了代码以反映 Tim 关于数据类型的评论。
You seem to be doing it a bit backward by converting everything to string multiple times, try something like this:
The documentation for TimeSpan.ToString is useful.
Edit: Updated the code to reflect Tim's comment about datatype.
试试这个:
Try this:
尝试使用 TimeSpan 对象,它应该为您完成所有艰苦的工作!
try using the TimeSpan object, it should do all the hard work for you!