1 常用时间函数 now(),date,time() , 数据类型是 date (本质cdbl)
本质数据类型是double:cdbl
- 本质上 now()=date + time()
- now() 包含完整的日期+时间, date是日期 和 time() 是时间
- 如果把时间转为 double 类型,就可以看出
- 数据类型虽然是日期型,但是本质是 double cdbl类型
- 整数部分就是 date ,而小数部分就是 time()
Sub daojishi11()
Debug.Print Now()
Debug.Print Date
Debug.Print Time()
Debug.Print CDbl(Now())
Debug.Print CDbl(Date)
Debug.Print CDbl(Time())
End Sub
2 timer() 定时器函数,单位是秒,可以认为是int类型
2.1 timer() 实测
- timer() 计时器函数
- 记录的是从一天零点开始到现在的秒数之和
- timer()的范围是0,24*60*60=86400
- 但是我不清楚用为什么 format(cdate(timer()),"hh:mm:ss")---转的时间不对,但是实际计算是从0点开始的秒数。
Sub daojishi10()
Debug.Print Now()
Debug.Print Date
Debug.Print Time()
Debug.Print Timer()
Debug.Print Format(CDate(Timer()), "hh:mm:ss")
End Sub
- timer() 直接用format() 转不对
2.2 timer()的用法
- timer()的作用: 定时做重复的事情?延时做指定事情?
- timer()的属性
- timer().enable 开启关闭定时器
- timer().interval 设定时间间隔 毫秒,
3 timer() 和 time 可以互转:(我觉得本质上 time= timer())
- timer() 是秒的计数,数据类型是 single
- time是double,日期类型,除以时间 timevalue() 就可以得到总秒数
- 而timer() 分别/3600 /60 就可以得到小时,分钟和剩下的 秒数
Sub daojishi11()
Debug.Print Now()
Debug.Print Date
Debug.Print Time()
Debug.Print CDbl(Now()) 'now() date time()本质是double
Debug.Print CDbl(Date)
Debug.Print CDbl(Time())
Debug.Print TypeName(Now())
Debug.Print TypeName(Date)
Debug.Print TypeName(Time())
Debug.Print TypeName(Timer())
'将double的time转为秒数
Debug.Print Time() / TimeValue("00:00:01")
'将秒数的timer()转为24小时制时间格式
t1 = Timer() '中间变量固定下来
h1 = Int(t1 / 3600)
m1 = Int((t1 - 3600 * h1) / 60)
s1 = t1 - 3600 * h1 - 60 * m1
Debug.Print t1 & "=" & h1 & ":" & m1 & ":" & s1
End Sub
可以测试,互转的结果, time=timer()
4 计算时间差
4.1 用 now() / time() 计算时间差
- 其实只算时间差的话,用 now() 和time() 也是一样的效果
- 时间类函数的时间差,直接得出的是 double类型的数,如果要显示为正常的时间:需要用format(转换)
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub daojishi1()
t1 = Now()
Debug.Print "t1=" & t1
Sleep 5000
t2 = Now() - t1
Debug.Print "t2=" & t2
Debug.Print "时间类函数的时间差,数据类型为date-double,需要转为时间格式才显示正常"
Debug.Print "t2=" & Format(CDate(t2), "hh:mm:ss")
Debug.Print "t2=" & Format(t2, "hh:mm:ss")
End Sub
4.2 用 timer() 计算时间差,直接返回 时间差为秒数,比较准。
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub daojishi5()
Debug.Print "开始运行"
t1 = Timer()
Sleep 5000
t2 = Timer() - t1
Debug.Print "时间过去了" & t2 & "秒"
Debug.Print "结束运行"
End Sub
5 timevalue() 和 timeSerial() 都是24小时制?
5.1 timevalue() 和 timeSerial()
- timevalue("") 参数是1个,是1个合理的时间字符串,不超过24小时,还可以用 am pm等
- timeserial(,,) 参数是3个,总时间也不能超过24小时,但分,秒可以大于60,会自动时间进位。
- timeserial(,,) 如果超过了24小时,会变成年月日,1899 12 31 是个时间设置起点?--真相是这已经是时间负数了!
Private Sub Constant_demo_Click()
Debug.Print (TimeValue("20:30"))
Debug.Print (TimeValue("5:15 am"))
Debug.Print (TimeValue("2:30:58"))
Debug.Print TimeValue("00:01:05 pm") '是24小时制的
' Debug.Print TimeValue("120 ") '必须是time_text的string文本,这样会报错
' Debug.Print (TimeValue("24:30:00")) '报错
Debug.Print TimeSerial(18, 59, 1)
Debug.Print TimeSerial(18, 179, 1) '一定范围的,分,秒,会自动进位,太大了超范围会报错
Debug.Print TimeSerial(23, 59, 59)
Debug.Print TimeSerial(24, 0, 0) '超过范围之后,会显示为年?--实际是时间负数!!
End Sub
5.2 datevalue() timevalue() cdate() 功能类似
Sub test910()
Debug.Print Now
Debug.Print CDate(Now)
Debug.Print DateValue(Now)
Debug.Print TimeValue(Now)
Debug.Print TimeValue("00:05:01")
Debug.Print CDate("00:05:01")
Debug.Print TimeSerial(0, 5, 1)
End Sub
5.3 VBA中的时间起点和负数
- 系统默认的日期最小值是0,即1900-1-0
- 所以时间上出现 小于 1900-1-0 就是负数了,比如上面例子的 1899-12-1
6 时间格式函数 format(,"yyyy:mm:dd hh:mm:ss")
- format(,"yyyy:mm:dd hh:mm:ss")
- 年份等写成几位就是几位
- 格式也可以用各种: - / 等等
Sub daojishi10()
Debug.Print Now()
Debug.Print Date
Debug.Print Time()
Debug.Print Timer()
Debug.Print Format(CDate(Timer()), "hh:mm:ss")
Debug.Print Format(Now, "yyyy:mm:dd hh:mm:ss")
Debug.Print Format(CDate(Now), "yy-mm-dd hh-mm-ss")
End Sub