当前位置:网站首页>Excel-vba quick start (XIII. Common usage of date)
Excel-vba quick start (XIII. Common usage of date)
2022-07-28 22:32:00 【Three uncle notes】
List of articles
1. Get system time
There are three common ways to obtain system time :
- Date Get the date of the current system time
- Time Get the hour, minute and second of the current system time
- Now Get the year, day, hour, minute and second of the current system time
Public Sub main()
Debug.Print Date ' result :2022/7/27
Debug.Print Time ' result :15:00:25
Debug.Print Now ' result :2022/7/27 15:00:25
End Sub
2. More detailed extraction time
- Extract year from date Year( Time )
- Extract the month from the date Month( Time )
- Extract the day from the date Day( Time )
- When extracted from the date Hour( Time )
- Extract minutes from the date VBA.Minute( Time )
- Extract seconds from dates Second( Time )
Public Sub main()
Debug.Print Year(Now) ' result :2022
Debug.Print Month(Now) ' result :7
Debug.Print Day(Now) ' result :27
Debug.Print Hour(Now) ' result :15
Debug.Print VBA.Minute(Now) ' result :4
Debug.Print Second(Now) ' result :20
End Sub
3. Format display date
The syntax of formatting date display is :Format( Time , Format ), Common formats are :
| Format | describe |
|---|---|
| yyyy-mm-dd | Specific date |
| yyyy year mm month dd Japan | Chinese date |
| yyyy year mm month dd Japan h:mm:ss | Chinese year month day hour minute second |
| d-mmm-yy | English month abbreviation |
| d-mmmm-yy | English month |
| aaaa | Chinese week |
| ddd | English week abbreviation |
| dddd | English week |
Example :
Public Sub main()
Debug.Print Format(Now, "yyyy-mm-dd") ' result :2022-07-27
Debug.Print Format(Now, "yyyy year mm month dd Japan ") ' result :2022 year 07 month 27 Japan
Debug.Print Format(Now, "yyyy year mm month dd Japan h:mm:ss") ' result :2022 year 07 month 27 Japan 15:13:31
Debug.Print Format(Now, "d-mmm-yy") ' result :27-Jul-22
Debug.Print Format(Now, "d-mmmm-yy") ' result :27-July-22
Debug.Print Format(Now, "aaaa") ' result : Wednesday
Debug.Print Format(Now, "ddd") ' result :Wed
Debug.Print Format(Now, "dddd") ' result :Wednesday
End Sub
3. Date difference calculation
The syntax of date difference calculation is :DateDiff( Time unit , Time , Time of comparison ):
Public Sub main()
Debug.Print DateDiff("d", "2022-07-27", "2022-07-28") ' How many days result :1
Debug.Print DateDiff("m", "2022-07-28", "2022-08-28") ' A few months away result :1
Debug.Print DateDiff("yyyy", "2022-07-28", "2023-07-28") ' A few years result :1
Debug.Print DateDiff("q", "2022-07-28", "2022-10-01") ' A few quarters apart result :1
Debug.Print DateDiff("w", "2022-07-28", "2022-08-04") ' The difference is several seven days result :1
Debug.Print DateDiff("h", "2022-07-28 15:00", "2022-07-28 16:00") ' A few hours apart result :1
Debug.Print DateDiff("n", "2022-07-28 15:01", "2022-07-28 15:02") ' A few minutes away result :1
Debug.Print DateDiff("s", "2022-07-28 15:01:00", "2022-07-28 15:01:01") ' A few seconds apart result :1
End Sub
4. Date addition and subtraction
The syntax for adding and subtracting dates is :DateAdd( Company , The time of addition and subtraction , Time ), When the time of addition and subtraction is positive, it is plus , A negative number is minus :
Public Sub main()
Debug.Print VBA.DateAdd("d", 10, "2022/07/27 16:00:00") ' Add ten days result :2022/8/6 16:00:00
Debug.Print VBA.DateAdd("m", 10, "2022/07/27 16:00:00") ' Add ten months result :2023/5/27 16:00:00
Debug.Print VBA.DateAdd("yyyy", 10, "2022/07/27 16:00:00") ' Plus ten years result :2032/7/27 16:00:00
Debug.Print VBA.DateAdd("yyyy", -10, "2022/07/27 16:00:00") ' Minus ten days result :2012/7/27 16:00:00
Debug.Print VBA.DateAdd("h", 10, "2022/07/27 16:00:00") ' Add ten hours result :2022/7/28 2:00:00
Debug.Print VBA.DateAdd("n", 10, "2022/07/27 16:00:00") ' Add ten minutes result :2022/7/27 16:10:00
Debug.Print VBA.DateAdd("s", 10, "2022/07/27 16:00:00") ' Add ten seconds result :2022/7/27 16:00:10
End Sub
5. Time serialization
Public Sub main()
Debug.Print VBA.DateSerial(2022, 7, 27) ' result :2022/7/27
Debug.Print VBA.TimeSerial(16, 1, 2) ' result :16:01:02
End Sub
边栏推荐
- Sword finger offer II 058. schedule (medium design segment tree treemap ordered set)
- Sword finger offer II 067. maximum XOR (medium prefix tree bit operation array)
- PaddleNLP基于ERNIR3.0文本分类以CAIL2018-SMALL数据集罪名预测任务为例【多标签】
- tutorial/detailed_workflow.ipynb 量化金融Qlib库
- What does GPRS network mean
- 76. Minimum coverage substring (hard sliding window hash table string)
- Why doesn't the icon on the elment plus icon input display
- Which is the file transfer command in the basic services of the Internet
- 静态路由和缺省路由实验
- 6K6w5LiA5qyh5pS75Ye75YiG5p6Q
猜你喜欢

HCIP(8)

HCIP(13)

LCR测试仪最为主要的功能和用途都是什么

What are the main functions and uses of LCR tester

SQL注入 Less42(POST型堆叠注入)

Changes in the history of oscilloscope development

JMeter installs third-party plug-ins plugins Manager
![[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints](/img/37/7cb5fa3a9078a5f5947485147c819d.png)
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints

Sword finger offer II 055. Binary search tree iterator (medium binary search tree iterator)

96. Different binary search trees (medium binary search tree dynamic planning)
随机推荐
JS array merging, de duplication, dimensionality reduction (es6: extended operator, set)
HCIP(13)
什么是时间复杂度
Why doesn't the icon on the elment plus icon input display
Sword finger offer II 057. the difference between the value and the subscript is within the given range (medium array bucket sort sliding window TreeSet)
LeetCode刷题系列之-多数之和类型
Common commands of NPM
乌官员:乌克兰一半农产品经多瑙河港口出口
MySQL command (add, delete, check and modify)
Is mov format a still image file format
mysql8.0无法给用户授权或提示You are not allowed to create a user with GRANT的问题
The binary search boundary value processing based on leetcode35 is used to clarify the boundary value of the judgment condition using the idea of interval
76. Minimum coverage substring (hard sliding window hash table string)
Hcip experiment (15)
删除容器镜像报错解决image is referenced in multiple repositories
Leetcode integer exercises integer inversion
How to install WiFi correctly
ECMASript 5/6 笔记
HCIP(15)
For loops and functions