当前位置:网站首页>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
边栏推荐
猜你喜欢

科大讯飞笔试
![[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints](/img/37/7cb5fa3a9078a5f5947485147c819d.png)
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
![[Ruiji takeout project] Day5 - Chapter 6 mobile verification code login](/img/53/c578e0d1428ea569fb412a20019924.png)
[Ruiji takeout project] Day5 - Chapter 6 mobile verification code login

Have you seen the management area decoupling architecture? Can help customers solve big problems

hcip实验(15)

LVS+KeepAlived高可用部署实战应用

Hcip experiment (15)

Bugku,Web:都过滤了

Brief introduction to PCB materials

Sword finger offer II 054. Sum of all values greater than or equal to nodes (medium binary search tree DFS)
随机推荐
tutorial/detailed_ workflow. Ipynb quantitative finance qlib Library
04. Default value of toref
阿里云CDN实践
Sword finger offer II 058. schedule (medium design segment tree treemap ordered set)
The person in charge of Tencent cloud database borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true
DHCP和PPPoE协议以及抓包分析
Win11怎么打开软件通知
【CVPR 2021】Cylinder3D:用于LiDAR点云分割的圆柱体非对称3D卷积网络
成立不到一年!MIT衍生量子计算公司完成900万美元融资
[NLP] generate word cloud
Sword finger offer II 052. flatten binary search tree (simple binary search tree DFS)
想要快速成长,先要经历重大打击!
HCIP(15)
AimBetter洞察您的数据库,DPM 和 APM 解决方案
Principle of object. Prototype. ToString. Call()
How to realize dynamic route switching and route caching in vuejs
The difference between get and post
SQL注入 Less34(POST型宽字节注入+布尔盲注)
MOV格式是不是静态图像文件格式
tutorial/detailed_workflow.ipynb 量化金融Qlib库