两次代码差异

时间:2015-01-29 03:13:59

标签: time vb6

我需要有关代码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 

可以帮助我plz thx

[编辑]

我试试这段代码但是效果不好 因为时间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

2 个答案:

答案 0 :(得分:2)

字符串不是数字,您不能添加或减去它们。在添加/减去它们之前,您需要先将它们转换为数字或日期

要添加/减去日期,您最好使用DateAdd()和DateDiff()函数

要反复检查某些内容,最好使用定时器控件

查看以下测试项目:

'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以获得其他结果

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

答案 1 :(得分:1)

您需要使用DateDiff功能。 您还希望将值存储在使用dim variableName as Date声明的变量中,而不是文本框中。您还想以某种方式对输入进行消毒,以便您的程序在第一次进入" bob"时不会崩溃。作为一个时间。