格式化时间 (HH:MM:SS)

发布于 2024-11-30 00:40:46 字数 504 浏览 1 评论 0原文

我从小时、分钟和秒的数字上升和下降中获取值。

问题是,例如,如果时间是上午 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝礼 2024-12-07 00:40:46

您似乎通过多次将所有内容转换为字符串来做到这一点,请尝试如下操作:

Dim ts As new TimeSpan(CInt(nmHour.Value), CInt(nmMin.Value), CInt(nmSec.Value))
lblST.Text = ts.ToString("HH:MM:SS")

TimeSpan.ToString 很有用。

编辑:更新了代码以反映 Tim 关于数据类型的评论。

You seem to be doing it a bit backward by converting everything to string multiple times, try something like this:

Dim ts As new TimeSpan(CInt(nmHour.Value), CInt(nmMin.Value), CInt(nmSec.Value))
lblST.Text = ts.ToString("HH:MM:SS")

The documentation for TimeSpan.ToString is useful.

Edit: Updated the code to reflect Tim's comment about datatype.

瑾兮 2024-12-07 00:40:46

试试这个:

Sub BtnSetClick(ByVal sender As Object, ByVal e As EventArgs)          

    lbl8.Visible = True          
    Dim nmTime As String = nmHour.Value.ToString().PadLeft(2, '0') + nmMin.Value.ToString().PadLeft(2, '0') + nmSec.Value.ToString().PadLeft(2, '0')            

    lblST.Text = nmTime          
    lblST.Visible = True      
End Sub 

Try this:

Sub BtnSetClick(ByVal sender As Object, ByVal e As EventArgs)          

    lbl8.Visible = True          
    Dim nmTime As String = nmHour.Value.ToString().PadLeft(2, '0') + nmMin.Value.ToString().PadLeft(2, '0') + nmSec.Value.ToString().PadLeft(2, '0')            

    lblST.Text = nmTime          
    lblST.Visible = True      
End Sub 
别念他 2024-12-07 00:40:46

尝试使用 TimeSpan 对象,它应该为您完成所有艰苦的工作!

Dim nmTime As New TimeSpan(nmHour.Value, nmMin.Value, nmSec.Value)

lblST.Text = nmTime.ToString
lblST.Visible = True

try using the TimeSpan object, it should do all the hard work for you!

Dim nmTime As New TimeSpan(nmHour.Value, nmMin.Value, nmSec.Value)

lblST.Text = nmTime.ToString
lblST.Visible = True
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文