当前位置:网站首页>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
边栏推荐
- tutorial/detailed_ workflow. Ipynb quantitative finance qlib Library
- DOM programming + events
- 微信小程序里button点击的时候会边框有黑线
- What is time complexity
- Closure, prototype and original link
- SQL injection less34 (post wide byte injection + Boolean blind injection)
- PaddleNLP基于ERNIR3.0文本分类:WOS数据集为例(层次分类)
- Solve Jupiter: the term 'Jupiter' is not recognized as the name of a cmdlet, function, script file
- LeetCode刷题系列之-多数之和类型
- 想要快速成长,先要经历重大打击!
猜你喜欢

Att & CK Threat Intelligence
![[CS231N]Lecture_2:Image Classification pipelin](/img/4f/de56b071560ada746c587a9dbc5f02.jpg)
[CS231N]Lecture_2:Image Classification pipelin

Ruiji takeout project - development of business development function Day2

6K6w5LiA5qyh5pS75Ye75YiG5p6Q

imx6q gpio复用

CMD common commands

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

Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million

Hcip experiment (14)

Soft exam network engineer
随机推荐
SSH password free login
Excel-VBA 快速上手(十三、日期的常见用法)
SQL injection less38 (Stack Injection)
6K6w5LiA5qyh5pS75Ye75YiG5p6Q
HCIP(9)
静态路由和缺省路由实验
What does GPRS network mean
SQL注入 Less38(堆叠注入)
Lotus 1.16.0 extend sector expiration time
The function of wechat applet to cut pictures
Static route and default route experiment
删除容器镜像报错解决image is referenced in multiple repositories
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
vuejs中如何实现动态路由切换及路由的缓存
Form validation and cascading drop-down lists (multiple implementations)
Idea generate class diagram plug-in UML (super detailed)
MySQL installation and configuration (super detailed, simple and practical)
使用webWorker执行后台任务
Static details of static members
成立不到一年!MIT衍生量子计算公司完成900万美元融资