vb6将列表中的时间与当前时间进行比较

时间:2014-09-08 09:34:35

标签: time vb6 compare

我正在制作表格,我需要输入每个时期的时间,例如。

  1. 开始时间 - 结束时间
  2. 开始时间 - 结束时间
  3. 开始时间 - 结束时间
  4. 这些记录可以像我的例子一样保存在列表框中。另一种形式我有3个标签 CurrentTime,TimePassed,TimeLeft和Timer,间隔为1秒。因此,在应用程序打开时,时间流逝,时间停止,当前时间显示正确的时间。

    在最后它看起来像这样

    enter image description here

    Private Sub cmdAdd_Click()
    Call addp
    End Sub
    
    Function addp(Optional ByVal t As String)
    Dim s As String, e As String
    If t = "" Then s = Trim(InputBox("Start time (hh:mm AM/PM):")) Else: s = Trim(Split(t, "-")(0))
    If s = "" Or InStr(1, s, " AM") = 0 And InStr(1, s, " PM") = 0 Then Exit Function
    If chk(s) = False Then Exit Function
    If t = "" Then e = Trim(InputBox(s & "-?" & vbCrLf & "End time:")) Else: e = Trim(Split(t, "-")(1))
    If e = "" Or InStr(1, e, " AM") = 0 And InStr(1, e, " PM") = 0 Then Exit Function
    If e = s Then Exit Function
    If chk(e) = False Then Exit Function
    If Format$(Split(s, "-")(0), "hh:mm AM/PM") > Format$(Split(e, "-")(0), "hh:mm AM/PM") Then Exit Function
    If lstPeriods.List(0) <> "" Then
    If Format$(Split(lstPeriods.List(lstPeriods.ListCount - 1), "-")(1), "hh:mm AM/PM") > Format$(Split(s, "-")(0), "hh:mm AM/PM") Then Exit Function
    End If
    lstPeriods.AddItem lstPeriods.ListCount + 1 & ". " & s & "-" & e
    If frmMain.lblPeriod.Caption = "" Then Call snd(s & "-" & e, lstPeriods.ListCount)
    End Function
    

    进行检查

    Function chk(ByVal st As String) As Boolean
    st = Replace$(Replace$(st, " AM", ""), " PM", "")
    If UBound(Split(st, ":")) <> 1 Then Exit Function
    For i = 0 To 1
    If IsNumeric(Split(st, ":")(i)) = False Then Exit Function
    If Len(Split(st, ":")(i)) <> 2 Then Exit Function
    If Split(st, ":")(i) < 0 Then Exit Function
    Next
    If Split(st, ":")(0) > 12 Then Exit Function
    If Split(st, ":")(1) > 59 Then Exit Function
    chk = True
    End Function
    

    我给出的解决方案是我唯一的初学者解决方案。而且我知道它很混乱而且很慢。使用trim / split / format是无法完成的,因为它需要进行大量修改。

    寻找更简单的解决方案。

    我需要将计算机上的当前时间与文本框/列表框中的人员时间进行比较,我该怎么做。

2 个答案:

答案 0 :(得分:1)

要运行此代码,您需要向Form1

添加5个控件
lblSystemTime = Label Control
lblTimeLeft = Label Control
lblTimePassed = Label Control
lblPeriod = Label Control

tmrSystemTime = Timer Control




Dim Periods()


Private Sub Form_Load()

Periods = Array( _
         "06:00 PM-07:00PM", _
        "07:01 PM-08:00PM", _
        "09:00 PM-10:00PM", _
        "1AM-2AM" _
        )


End Sub

Private Sub tmrSystemTime_Timer()

lblSystemTime.Caption = FormatDateTime(Now, vbLongTime)

 Dim OnPeriod As Integer
 OnPeriod = GetPeriod()


If OnPeriod < 0 Then

    lblTimeLeft.Caption = vbNullString
    lblTimePassed.Caption = vbNullString
    lblPeriod.Caption = "Unknown Period"

Else
    lblPeriod = CStr(OnPeriod + 1) & ". period"

    lblTimeLeft.Caption = "Time Left: " & Format( _
                                            DateAdd("s", _
                                                    DateDiff("s", _
                                                        CDate(lblSystemTime.Caption), _
                                                        CDate(Split(Periods(OnPeriod), "-")(1))), _
                                                            CDate("0") _
                                                                    ), _
                                                                    "nn:ss" _
                                                                     )





    lblTimePassed.Caption = "Time Passed: " & Format( _
                                            DateAdd("s", _
                                                    DateDiff("s", _
                                                        CDate(Split(Periods(OnPeriod), "-")(1)), _
                                                        CDate(lblSystemTime.Caption)), _
                                                            CDate("0") _
                                                                    ), _
                                                                    "nn:ss" _
                                                                     )





End If



End Sub

Private Function GetPeriod() As Integer

Dim ICount As Integer
For Each Pr In Periods

    If CDate(Split(Pr, "-")(0)) <= CDate(lblSystemTime.Caption) And _
        CDate(Split(Pr, "-")(1)) >= CDate(lblSystemTime.Caption) Then

        GetPeriod = ICount
        Exit Function
    End If


ICount = ICount + 1
Next



GetPeriod = -1

End Function

答案 1 :(得分:1)

这可能有所帮助:

Private Sub TimeCheck(byval int_TimeNow as Integer)
  dim intTimeThen as string 'set a variable to compare for the THEN time
  dim intTimeNow as string 'set a variable to compare for the NOW time
  dim intDiff as string ' set a variable for the difference in time

  intTimeNow = int_TimeNow 'just to ensure we don't mess with any data

  intTimeThen = val(txtTimeThen.text) 'get the numeric value of the time, the system will  
                                    'convert it from a string to the value here.

  intDiff = intTimeThen - intTimeNow 'Do the math

  lstTimeEvents.additem timevalue(intDiff) 'write it to a listbox
End Sub

我意识到这不是您正在使用的编码约定,但它应该在某种程度上展示您正在寻找的内容。