使用vb6添加前导零?

时间:2014-06-03 13:45:08

标签: vb6

我在尝试这样做时无处可去,到目前为止,我已经做到了这一点,

Private Sub Timeperpart_Timer()


    secs = secs + 1
    If secs = 60 Then
    mins = mins + 1
    secs = 0
    End If
    If mins = 60 Then
    hrs = hrs + 1
    mins = 0
    End If

     If secs < 10 Then
           Lbltime.Caption = CStr(hrs & ":" & mins & ":" & 0 & CStr(secs))
           End If

If mins < 10 Then
ElseIf secs < 10 Then
            Lbltime.Caption = CStr(hrs & ":" & 0 & CStr(mins) & ":" & 0 & CStr(secs))
      Else
       Lbltime.Caption = CStr(hrs & ":" & 0 & CStr(mins) & ":" & secs)
       End If

       If hrs < 10 Then
       ElseIf mins < 10 Then
       ElseIf secs < 10 Then
       Lbltime.Caption = CStr(0 & CStr(hrs) & ":" & 0 & CStr(mins) & ":" & 0 & CStr(secs))
       End If

       If hrs < 10 Then
       ElseIf mins < 10 Then
       ElseIf secs > 10 Then

        Lbltime.Caption = CStr(0 & CStr(hrs) & ":" & 0 & CStr(mins) & ":" & secs)
       End If

        If hrs < 10 Then
       ElseIf mins > 10 Then
     ElseIf secs > 10 Then

        Lbltime.Caption = CStr(0 & CStr(hrs) & ":" & mins & ":" & secs)
       End If

End Sub

问题是,在行if mins < 10 then中,程序不会将mins识别为小于10,如果它为零,则不会添加前导零,与小时相同。我正在寻找一种有效的方式,或者只是以任何方式来做到这一点。

1 个答案:

答案 0 :(得分:5)

将所有代码替换为:

Lbltime.Caption = Format$(TimeSerial(hrs, mins, secs), "hh:mm:ss")

事实上,还要将所有hrs mins secs添加逻辑替换为:

Dim the_time As Date
....
the_time = DateAdd("s", 1, the_time)

所以最后你有:

Private the_time As Date

Private Sub Timeperpart_Timer()
  the_time = DateAdd("s", 1, the_time)
  Lbltime.Caption = Format$(the_time, "hh:mm:ss")
End Sub