当前位置:网站首页>Excel-VBA 快速上手(十三、日期的常见用法)
Excel-VBA 快速上手(十三、日期的常见用法)
2022-07-28 20:41:00 【三叔笔记】
1. 获取系统时间
常见的三个获取系统时间的方式:
- Date 获取当前系统时间的年月日
- Time 获取当前系统时间的时分秒
- Now 获取当前系统时间的年月日和时分秒
Public Sub main()
Debug.Print Date '结果:2022/7/27
Debug.Print Time '结果:15:00:25
Debug.Print Now '结果:2022/7/27 15:00:25
End Sub
2. 更细力度的提取时间
- 从日期中提取年 Year(时间)
- 从日期中提取月 Month(时间)
- 从日期中提取日 Day(时间)
- 从日期中提取时 Hour(时间)
- 从日期中提取分 VBA.Minute(时间)
- 从日期中提取秒 Second(时间)
Public Sub main()
Debug.Print Year(Now) '结果:2022
Debug.Print Month(Now) '结果:7
Debug.Print Day(Now) '结果:27
Debug.Print Hour(Now) '结果:15
Debug.Print VBA.Minute(Now) '结果:4
Debug.Print Second(Now) '结果:20
End Sub
3. 格式化显示日期
格式化日期显示的语法为:Format(时间, 格式),常见格式有:
| 格式 | 描述 |
|---|---|
| yyyy-mm-dd | 年月日 |
| yyyy年mm月dd日 | 中文年月日 |
| yyyy年mm月dd日 h:mm:ss | 中文年月日时分秒 |
| d-mmm-yy | 英文月份简写 |
| d-mmmm-yy | 英文月份 |
| aaaa | 中文星期 |
| ddd | 英文星期简写 |
| dddd | 英文星期 |
示例:
Public Sub main()
Debug.Print Format(Now, "yyyy-mm-dd") '结果:2022-07-27
Debug.Print Format(Now, "yyyy年mm月dd日") '结果:2022年07月27日
Debug.Print Format(Now, "yyyy年mm月dd日 h:mm:ss") '结果:2022年07月27日 15:13:31
Debug.Print Format(Now, "d-mmm-yy") '结果:27-Jul-22
Debug.Print Format(Now, "d-mmmm-yy") '结果:27-July-22
Debug.Print Format(Now, "aaaa") '结果:星期三
Debug.Print Format(Now, "ddd") '结果:Wed
Debug.Print Format(Now, "dddd") '结果:Wednesday
End Sub
3. 日期差计算
日期差计算语法为:DateDiff(时间单位, 时间, 比较的时间):
Public Sub main()
Debug.Print DateDiff("d", "2022-07-27", "2022-07-28") '相差几天 结果:1
Debug.Print DateDiff("m", "2022-07-28", "2022-08-28") '相差几月 结果:1
Debug.Print DateDiff("yyyy", "2022-07-28", "2023-07-28") '相差几年 结果:1
Debug.Print DateDiff("q", "2022-07-28", "2022-10-01") '相差几个季度 结果:1
Debug.Print DateDiff("w", "2022-07-28", "2022-08-04") '相差几个七天 结果:1
Debug.Print DateDiff("h", "2022-07-28 15:00", "2022-07-28 16:00") '相差几小时 结果:1
Debug.Print DateDiff("n", "2022-07-28 15:01", "2022-07-28 15:02") '相差几分钟 结果:1
Debug.Print DateDiff("s", "2022-07-28 15:01:00", "2022-07-28 15:01:01") '相差几秒钟 结果:1
End Sub
4. 日期加减
日期加减语法为:DateAdd(单位, 加减的时间, 时间),加减的时间为正数就是加,为负数就是减:
Public Sub main()
Debug.Print VBA.DateAdd("d", 10, "2022/07/27 16:00:00") '加十天 结果:2022/8/6 16:00:00
Debug.Print VBA.DateAdd("m", 10, "2022/07/27 16:00:00") '加十个月 结果:2023/5/27 16:00:00
Debug.Print VBA.DateAdd("yyyy", 10, "2022/07/27 16:00:00") '加十年 结果:2032/7/27 16:00:00
Debug.Print VBA.DateAdd("yyyy", -10, "2022/07/27 16:00:00") '减十天 结果:2012/7/27 16:00:00
Debug.Print VBA.DateAdd("h", 10, "2022/07/27 16:00:00") '加十小时 结果:2022/7/28 2:00:00
Debug.Print VBA.DateAdd("n", 10, "2022/07/27 16:00:00") '加十分钟 结果:2022/7/27 16:10:00
Debug.Print VBA.DateAdd("s", 10, "2022/07/27 16:00:00") '加十秒钟 结果:2022/7/27 16:00:10
End Sub
5. 时间序列化
Public Sub main()
Debug.Print VBA.DateSerial(2022, 7, 27) '结果:2022/7/27
Debug.Print VBA.TimeSerial(16, 1, 2) '结果:16:01:02
End Sub
边栏推荐
猜你喜欢

SQL injection less38 (Stack Injection)

C语言编程规范学习笔记和总结

内网渗透学习(三)域横向移动——计划任务

Overall introduction of Ruiji takeout project

mysql create语句能不能用来建立表结构并追加新的记录

Sword finger offer II 052. flatten binary search tree (simple binary search tree DFS)

Embrace open source guidelines

腾讯云数据库负责人林晓斌借一亿元炒股?知情人士:金额不实

HCIP第七次实验

Hcip experiment (12)
随机推荐
LVS+KeepAlived高可用部署实战应用
Using Baidu easydl to realize chef hat recognition of bright kitchen and stove
display 各值的区别
Hcip experiment (15)
【机器学习】朴素贝叶斯对文本分类--对人名国别分类
gprs网络指的是什么
TensorFlow Serving 高性能的机器学习模型服务系统
Hcip experiment (12)
2021年数学建模B组代码
CDN working principle
Chapter 7: drawing rotating cubes
静态路由和缺省路由实验
lotus 1.16.0 延长扇区过期时间
Aimbetter insight into your database, DPM and APM solutions
Win11 how to open software notification
Bugku,Web:都过滤了
Day3 classification management of Ruiji takeout project
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
Static route and default route experiment
Which is the file transfer command in the basic services of the Internet