根据VB中date型数据的存储格式,(浮点数,小数点前为日期,后为时间)很容易想到日期的推迟或置前运算可由简单的算术运算语句 date1 = date + n 或date1 = date - n(n为天数)来实现
由此想到,时间的前移后移运算可否也用比较简单的方法来实现?我现在要编一个倒退n秒的程序,不得不去写一个专用子程序,通过取秒减n,还要依次判是否向高位(分位,小时位,日期位)借位,显得比较烦琐,能否通过比较简练的语句来实现?望高手指点

解决方案 »

  1.   

    我有一个到记时程序
    Dim Hours As Integer
    Dim Minutes As Integer
    Dim Seconds As Integer
    Dim Time As DatePrivate Sub Mydisplay()Hours = Val(Text1.Text)
        Minutes = Val(Text2.Text)
        Seconds = Val(Text3.Text)
    Time = TimeSerial(Hours, Minutes, Seconds)    Label1.Caption = Format$(Time, "hh") & ":" & Format$(Time, "nn") & ":" & Format$(Time, "ss")
    End SubPrivate Sub Command1_Click()    Timer1.Enabled = True
        Command3.Enabled = False
    End SubPrivate Sub Command2_Click()
    Timer1.Enabled = False
        Command3.Enabled = True
    End SubPrivate Sub Command3_Click()
    Hours = 0
        Minutes = 0
        Seconds = 0
        Time = 0
        Text1.Text = " "
        Text2.Text = " "
        Text3.Text = " "
        Text1.SetFocus 'put curser in the hour text box
    End SubPrivate Sub Command4_Click()
    End
    End SubPrivate Sub Form_Load()
    Form1.Top = (Screen.Height - Form1.Height) / 2
    Form1.Left = (Screen.Width - Form1.Width) / 2
    Timer1.Interval = 1000
        Hours = 0
        Minutes = 0
        Seconds = 0
        Time = 0
    End SubPrivate Sub Text1_Change()
    Mydisplay
    End SubPrivate Sub Text2_Change()
    Mydisplay
    End SubPrivate Sub Text3_Change()
    Mydisplay
    End SubPrivate Sub Timer1_Timer()Timer1.Enabled = False
        If (Format$(Time, "hh") & ":" & Format$(Time, "nn") & ":" & Format$(Time, "ss")) <> "00:00:00" Then 'Counter to continue loop until 0
            Time = DateAdd("s", -1, Time)
            Label1.Visible = False
            Label1.Caption = Format$(Time, "hh") & ":" & Format$(Time, "nn") & ":" & Format$(Time, "ss")
            Label1.Visible = True
            Timer1.Enabled = True
        ElseTimer1.Enabled = False
            Beep
            Beep
            Command3.Enabled = True
        End If
    End Sub
    你要的话
    eml::
      

  2.   

    我看过你粘的代码,看来关键函数就是Dateadd了,
    如能相赠,非常感谢(第2个参数为正是否就可以正计时了呢?)