当前位置:网站首页>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
边栏推荐
- [Ruiji takeout project]day4 - dish management
- Which is the file transfer command in the basic services of the Internet
- [leetcode] maximum depth of binary tree
- Netease Yunxin 2022q2 product supply station, come and get your product supply plan!
- Is mov format a still image file format
- HCIP(8)
- 容器化配置启动redis集群 单机6节点 3主3从
- npm ERR code ETIMEDOUT npm ERR syscall connect npm ERR errno ETIMEDOUT npm ERR network reques...
- Static details of static members
- Win11怎么打开软件通知
猜你喜欢
随机推荐
npm ERR code ETIMEDOUT npm ERR syscall connect npm ERR errno ETIMEDOUT npm ERR network reques...
[leetcode] maximum depth of binary tree
When can I sign up for the 2022 class I constructor examination?
HCIP(11)
Necessary for in-depth learning: split the data set, split the labels according to the split pictures, and check the interval of all marked labels
SQL注入 Less38(堆叠注入)
winServer运维技术栈
Day3 classification management of Ruiji takeout project
What does GPRS network mean
Chrome encountered a problem when debugging the code. After modifying and saving the code in vscode, chrome did not update after refreshing
Sword finger offer II 056. Sum of two nodes in a binary search tree (simple binary search tree DFS hash table double pointer iterator)
Analysis notes on let (const) temporary dead zone in JS
Lin Xiaobin, head of Tencent cloud database, borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true
Hcip seventh experiment
105. Construct binary tree from preorder and inorder traversal sequence (medium binary tree DFS hash table binary tree)
[CS231N]Lecture_2:Image Classification pipelin
Lotus 1.16.0 extend sector expiration time
Netease Yunxin 2022q2 product supply station, come and get your product supply plan!
flask之蓝图 补充openpyxl
PaddleNLP基于ERNIR3.0文本分类以CAIL2018-SMALL数据集罪名预测任务为例【多标签】








