VB中如何把时间自动取整到整点

来源:百度知道 编辑:UC知道 时间:2024/05/22 19:33:35
例如:
现在时间是8:09,就把时间变量等于8点整
现在时间是8:51,就把时间变量等于9点整
就是在整点的正负10分钟内都取整到整点。

这有何难?自己计算一下就好了..把下面的 copy 到你vb里运行一下就明白了:

dim myTime,myNewTime

myTime=split(cstr(Time()),":")

if val(myTime(1))>50 then
myNewTime=val(myTime(0))+1 & ":00"
elseif val(myTime(1))<10 then
myNewTime=myTime(0)& ":00"
else
myNewTime=myTime(0)& ":"&myTime(1)
end if

msgbox("现在时刻:" + myNewTime)

自己写函数吧,没有直接的方法。
给个例子你:
function GetIntHour() as integer
返回整点,如果不是正点10分钟范围内,返回-1
Dim t_now As Date, t_hour As Integer
t_now = Now()
t_hour = Hour(t_now)
If t_now > CDate(Date & " " & t_hour & ":50") Then
t_hour = t_hour + 1
ElseIf t_now > CDate(Date & " " & t_hour & ":10") Then
t_hour = -1
End If
If t_hour>=24 Then t_hour=0
GetIntHour=t_hour
end function