两次代码差异

Code difference between the two times

我需要有关代码 vb6 的帮助 我需要这样的代码

t1.text=now ' example "10:30:00 PM"
t2.text="1:00:00 am"  ' that next day and i can chang him
t3.text=t2.text-t1.text ' he most be "2:30:00"and Countdown
'and i need code to + hour amd minute 
' example 
't2.text is "1:00:00" + new time  "1:30:00" = "2:30:00"
't3.text most be "3:00:00" and Countdown 

能帮我吗谢谢

[编辑]

我试过这段代码,但效果不佳 因为今天是 text3,后天是 text4

Dim timein As Date
Dim timeout As Date
Dim v_diff As Date

Private Sub Timer3_Timer()
text4.text="1:00:00am"
Text3.Text = Format(Time, "HH:MM:SS AMPM")
timein = Text4.Text
timeout = Text3.Text
v_diff = (timein - timeout)
'*********************************************
Hourdiff = Hour(v_diff)
minutediff = Minute(v_diff)
seconddiff = Second(v_diff)
'*********************************************
Text5.Text = Hourdiff & " : " & minutediff & " : " & seconddiff
Label2.Caption = Text5.Text

您需要使用DateDiff功能。 您还希望将值存储在使用 dim variableName as Date 声明的变量中,而不是文本框。您还需要以某种方式清理输入,这样您的程序就不会在有人第一次输入 "bob" 时崩溃。

字符串不是数字,不能加减。您需要先将它们转换为数字或日期,然后才能 add/substract 它们

至 add/substract 日期,您最好使用 DateAdd() 和 DateDiff() 函数

要反复检查某些东西,最好使用 Timer 控件

看看下面的测试项目:

'1 form with
'  1 textbox        : name=Text1
'  1 textbox        : name=Text2
'  1 label          : name=Label1
'  1 command button : name=Command1
Option Explicit

Private Sub Command1_Click()
  'show difference between text1 and text2
  ShowDiff Text1.Text, Text2.Text
End Sub

Private Sub Timer1_Timer()
  'show countdown from now till text2
  ShowDiff CStr(Now), Text2.Text
End Sub

Private Sub Form_Load()
  'load some values
  Text1.Text = CStr(Now)
  Text2.Text = CStr(DateAdd("d", 1, Now))
  'configure timer to show difference every second
  With Timer1
    .Interval = 1000
    .Enabled = True
  End With 'Timer1
End Sub

Private Sub Form_Resize()
  Dim sngWidth As Single, sngHeight As Single
  sngWidth = ScaleWidth
  sngHeight = ScaleHeight / 4
  Text1.Move 0, 0, sngWidth, sngHeight
  Text2.Move 0, sngHeight, sngWidth, sngHeight
  Label1.Move 0, 2 * sngHeight, sngWidth, sngHeight
  Command1.Move 0, 3 * sngHeight, sngWidth, sngHeight
End Sub

Private Sub ShowDiff(strStart As String, strEnd As String)
  Dim datStart As Date, datEnd As Date
  Dim lngMin As Long
  Dim strDiff As String
  'convert strings to date types
  datStart = CDate(strStart)
  datEnd = CDate(strEnd)
  'calculate difference in minutes
  lngMin = DateDiff("n", datStart, datEnd)
  'hours
  strDiff = CStr(lngMin \ 60)
  'minutes
  strDiff = strDiff & ":" & Right$("0" & CStr(lngMin Mod 60), 2)
  'show difference as hours:minutes
  Label1.Caption = strDiff
End Sub

在 Text1 中从当前时间开始,在 Text2 中从明天的同一时间开始

当您点击按钮时,Text1 和 Text2 之间的差异将显示在 Label1 中.. 开始时始终是 24:00,但您可以更改 Text1 和 Text2 以获得其他结果

定时器每秒都会处理它的事件,这将显示从当前时间到 Text2 中的时间的倒计时。每秒这将覆盖 Label1 中显示的任何内容