date()

返回当前的系统日期

返回格式为 YYYY/MM/DD

CDate()

学习资料:https://www.yiibai.com/vba/vba_cdate_function.html

将有效的日期和时间表达式转换为类型日期。

用法

cdate(date)

丸子:就是把输入转换为固定日期格式: YYYY/MM/DD

支持“月日年”、“年月日”格式,其中月份可以为英文缩写,但是 Libre Office 的编辑器不支持此种格式,会报错,只能使用 Micro Office Excel 程序自带开发工具。

  1. Private Sub Constant_demo_Click()
  2. Dim a As Variant
  3. Dim b As Variant
  4. a = CDate("Jan 01 2020")
  5. MsgBox ("The Value of a : " & a)
  6. b = CDate("31 Dec 2050")
  7. MsgBox ("The Value of b : " & b)
  8. c = CDate("2020-02-27")
  9. MsgBox ("The Value of c : " & c)
  10. End Sub

DateAdd()

学习材料:https://www.yiibai.com/vba/vba_dateadd_function.html

返回一个指定的时间间隔被添加的日期。

用法

DateAdd(interval,number,date)

  1. Private Sub date_demo_Click()
  2. ' Positive Interal
  3. date1 = 23 - Jan - 2020
  4. MsgBox ("yyyy - 年份: " & DateAdd("yyyy", 1, date1))
  5. MsgBox ("q - 季度: " & DateAdd("q", 1, date1))
  6. MsgBox ("m - 一年中的月份: " & DateAdd("m", 1, date1))
  7. MsgBox ("y - 一年中的年份: " & DateAdd("y", 1, date1))
  8. MsgBox ("d - 一年中的一天: " & DateAdd("d", 1, date1))
  9. MsgBox ("w - 工作日: " & DateAdd("w", 1, date1))
  10. MsgBox ("ww - 星期: " & DateAdd("ww", 1, date1))
  11. MsgBox ("h - 小时: " & DateAdd("h", 1, "01-Jan-2013 12:00:00"))
  12. MsgBox ("n - 分钟: " & DateAdd("n", 1, "01-Jan-2013 12:00:00"))
  13. MsgBox ("s - 秒钟: " & DateAdd("s", 1, "01-Jan-2013 12:00:00"))
  14. ' Negative Interval
  15. MsgBox ("yyyy - 年份: " & DateAdd("yyyy", -1, date1))
  16. MsgBox ("q - 季度: " & DateAdd("q", -1, date1))
  17. MsgBox ("m - 一年中的月份: " & DateAdd("m", -1, date1))
  18. MsgBox ("y - 一年中的年份: " & DateAdd("y", -1, date1))
  19. MsgBox ("d - 一年中的一天: " & DateAdd("d", -1, date1))
  20. MsgBox ("w - 工作日: " & DateAdd("w", -1, date1))
  21. MsgBox ("ww - 星期: " & DateAdd("ww", -1, date1))
  22. MsgBox ("h - 小时: " & DateAdd("h", -1, "01-Jan-2013 12:00:00"))
  23. MsgBox ("n - 分钟: " & DateAdd("n", -1, "01-Jan-2013 12:00:00"))
  24. MsgBox ("s - 秒钟 : " & DateAdd("s", -1, "01-Jan-2013 12:00:00"))
  25. End Sub

DateDiff

学习材料:https://www.yiibai.com/vba/vba_datediff_function.html

返回两个指定的时间间隔之间的差值。

丸子:不太明白一年中的年和天是啥意思

用法

DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]])

  1. Private Sub Constant_demo_Click()
  2. Dim fromDate As Variant
  3. fromDate = "01-Jan-2009 00:00:00"
  4. Dim toDate As Variant
  5. toDate = "01-Jan-2010 23:59:00"
  6. MsgBox ("yyyy - 年份: " & DateDiff("yyyy", fromDate, toDate))
  7. MsgBox ("q - 季度: " & DateDiff("q", fromDate, toDate))
  8. MsgBox ("m - 一年中的月份: " & DateDiff("m", fromDate, toDate))'给的参数里面有俩 m
  9. MsgBox ("y - 一年中的年份: " & DateDiff("y", fromDate, toDate))
  10. MsgBox ("d - 一年中的一天:" & DateDiff("d", fromDate, toDate))
  11. MsgBox ("w - 工作日: " & DateDiff("w", fromDate, toDate))
  12. MsgBox ("ww - 星期: " & DateDiff("ww", fromDate, toDate))
  13. MsgBox ("h - 小时: " & DateDiff("h", fromDate, toDate))
  14. MsgBox ("n - 分钟: " & DateDiff("n", fromDate, toDate))'给的参数里面没有 n
  15. MsgBox ("s - 秒钟: " & DateDiff("s", fromDate, toDate))
  16. End Sub

DatePart()

学习资料:https://www.yiibai.com/vba/vba_datepart_function.html

返回给定日期的特定部分。

用法

DatePart(interval,date[,firstdayofweek[,firstweekofyear]])

  1. Private Sub DatePart_demo_Click()
  2. Dim Quarter As Variant
  3. Dim DayOfYear As Variant
  4. Dim WeekOfYear As Variant
  5. Date1 = "2020-02-27"
  6. Quarter = DatePart("q", Date1)
  7. MsgBox ("q - 季度: " & Quarter)
  8. DayOfYear = DatePart("y", Date1)
  9. MsgBox ("y - 一年中的年份: " & DayOfYear)
  10. WeekOfYear = DatePart("ww", Date1)
  11. MsgBox ("ww - 星期: " & WeekOfYear)
  12. MsgBox ("m - 一年中的月份: " & DatePart("m", Date1))
  13. End Sub

DateSerial()

学习资料:https://www.yiibai.com/vba/vba_dateserial_function.html

返回指定日期、月份和年份参数的日期。

丸子:分别输入年月日,返回固定格式 YYYY/MM/DD

用法

DateSerial(year,month,day)

  1. Private Sub Constant_demo_Click()
  2. msgbox(DateSerial(2020,2,27))
  3. End Sub

FormatDateTime()

学习资料:https://www.yiibai.com/vba/vba_formatdatetime_function.html

根据提供的参数格式化日期。

用法

FormatDateTime(date,format)

  1. Private Sub FormatDateTimedemo()
  2. d = ("2020-02-27 12:32")
  3. MsgBox ("0 = vbGeneralDate - 默认值: " & FormatDateTime(d))
  4. MsgBox ("1 = vbLongDate - 返回长日期: " & FormatDateTime(d, 1))
  5. MsgBox ("2 = vbShortDate - 返回短日期: " & FormatDateTime(d, 2))
  6. MsgBox ("3 = vbLongTime - 返回长时间: " & FormatDateTime(d, 3))
  7. MsgBox ("4 = vbShortTime - 返回短时间: " & FormatDateTime(d, 4))
  8. End Sub

VBA 学习笔记 - 日期的更多相关文章

  1. VBA学习笔记

    这是一个学习VBA编程的学习笔记. 一. 介绍 二. 使用手册 2.1. 如何在Excel2010中开始使用VBA? 2.2. 如何使用VBA编辑器进行编程? 三. 语法说明 3.1 数据类型 3.2 ...

  2. VBA 学习笔记 - 变量与常量

    学习资料:https://www.yiibai.com/vba/vba_variables.html 变量和常量命名规则 必须以字母开头 不能包含空格.句点(.).感叹号(!)或字符@,&,$ ...

  3. VBA 学习笔记 - 输入框

    学习资料 https://www.yiibai.com/vba/vba_input_box.html 输入框 InputBox 函数说明 提示用户输入值.当输入值后,如果用户单击确定 按钮或按下键盘上 ...

  4. VBA 学习笔记 - 运算符

    学习资料:https://www.yiibai.com/vba/vba_operators.html 算术运算符 加减乘除模指,这个没啥特别的. 比较运算符 和 Lua 相比,判断相等变成了一个等于号 ...

  5. VBA 学习笔记 - 消息框

    学习资料:https://www.yiibai.com/vba/vba_macro_comments.html 注释 单引号或 REM 开头 丸子:多行注释咋办? 消息框(MsgBox) 函数功能:显 ...

  6. SQLServer学习笔记<>日期和时间数据的处理(cast转化格式、日期截取、日期的加减)和 case表达式

    日期和时间数据的处理. (1)字符串日期 ‘20080301’,这一串为字符串日期,但必须保证为四位的年份,两位的月份,两位的日期.例如,查询订单表日期大于‘20080301’.可以这样写: 1 se ...

  7. php学习笔记——日期和时间

    一.time() 来取得服务器当前时间的时间戳 UNIX 时间戳(timestamp)是 PHP 中关于时间日期一个很重要的概念,它表示从 1970年1月1日 00:00:00 到当前时间的秒数之和. ...

  8. VBA学习笔记(8)--遍历所有文件夹和文件

    说明(2017.3.26): 1. 采用的是兰色幻想教学视频中的“父子转换法” 2. 这种VBA的遍历文件夹方法非常难理解,主要是因为dir这个函数,第一次带参数调用,返回的是此目录下的第一个文件,第 ...

  9. VBA学习笔记(4)--数组和单元格互相转换

    说明(2017.3.23): 1. VBA的数组还是很难用的,其实就是非常难用! 2. 要先定义一个数组,可以是空的,也可以里面写个数字作为数组长度. 3. 如果是空数组,可以后面redim重新定义数 ...

随机推荐

  1. Request继承体系

    ServletRequest——接口 ↑继承 HttpServletRequest——接口 ↑实现 org.apache.catalina.connector.RequestFacade——类(Tom ...

  2. &nbsp;&laquo;&raquo;&lt;&gt;

     空格   «双小于 »双大于 <小于 >大于

  3. Goahead WebSever 总结

    编译成功后用http://127.0.0.1可以访问网站,若端口号不是默认的80,者访问时加“:端口”,如: http://127.0.0.1:8888 1.websHomePageHandler函数 ...

  4. docker dial unix /var/run/docker.sock: connect: permission denied

    Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker. ...

  5. C语言合法标识符 题解

    输入一个字符串,判断其是否是C的合法标识符.  Input输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串. Output对 ...

  6. ES5中, map 和 forEach的区别

    forEach和map区别在哪里知道吗? // forEach Array.prototype.forEach(callback(item, index, thisArr), thisArg) // ...

  7. C 语言实现面向对象编程

    转载 https://blog.csdn.net/onlyshi/article/details/81672279 C 语言实现面向对象编程1.引言面向对象编程(OOP)并不是一种特定的语言或者工具, ...

  8. 【安卓逆向】反编译ELF的另类技巧

    IDA 反编译 ObjDump反编译 ObjDump是ndk环境自带的一个脚本,在android-ndk-r10c/toolchains/arm-linux-androideabi-4.9/prebu ...

  9. linux安装nginx以及如何启动,暂停,停止操作

    链接:https://www.cnblogs.com/martinl/p/10908607.html 命令kill -9 pid杀死进程,pid是系统的父进程号 Ubuntu下载nginx:https ...

  10. Crawlab-分布式爬虫管理系统

    一.简介 Crawlab 基于Celery的爬虫分布式爬虫管理平台,支持多种编程语言以及多种爬虫框架. Github: https://github.com/tikazyq/crawlab 参考资料 ...