zoukankan      html  css  js  c++  java
  • VBA 学习笔记 日期

    date()

    返回当前的系统日期
    返回格式为 YYYY/MM/DD

    CDate()

    学习资料:https://www.yiibai.com/vba/vba_cdate_function.html
    将有效的日期和时间表达式转换为类型日期。

    用法

    cdate(date)
    丸子:就是把输入转换为固定日期格式: YYYY/MM/DD
    支持“月日年”、“年月日”格式,其中月份可以为英文缩写,但是 Libre Office 的编辑器不支持此种格式,会报错,只能使用 Micro Office Excel 程序自带开发工具。

    Private Sub Constant_demo_Click()
       Dim a As Variant
       Dim b As Variant
    
       a = CDate("Jan 01 2020")
       MsgBox ("The Value of a : " & a)
    
       b = CDate("31 Dec 2050")
       MsgBox ("The Value of b : " & b)
    
       c = CDate("2020-02-27")
       MsgBox ("The Value of c : " & c)
    End Sub
    

    DateAdd()

    学习材料:https://www.yiibai.com/vba/vba_dateadd_function.html
    返回一个指定的时间间隔被添加的日期。

    用法

    DateAdd(interval,number,date)

    Private Sub date_demo_Click()
    
       ' Positive Interal
       date1 = 23 - Jan - 2020
       MsgBox ("yyyy - 年份: " & DateAdd("yyyy", 1, date1))
       MsgBox ("q - 季度: " & DateAdd("q", 1, date1))
       MsgBox ("m - 一年中的月份: " & DateAdd("m", 1, date1))
       MsgBox ("y - 一年中的年份: " & DateAdd("y", 1, date1))
       MsgBox ("d - 一年中的一天: " & DateAdd("d", 1, date1))
       MsgBox ("w - 工作日: " & DateAdd("w", 1, date1))
       MsgBox ("ww - 星期: " & DateAdd("ww", 1, date1))
       MsgBox ("h - 小时: " & DateAdd("h", 1, "01-Jan-2013 12:00:00"))
       MsgBox ("n - 分钟: " & DateAdd("n", 1, "01-Jan-2013 12:00:00"))
       MsgBox ("s - 秒钟: " & DateAdd("s", 1, "01-Jan-2013 12:00:00"))
    
       ' Negative Interval
       MsgBox ("yyyy - 年份: " & DateAdd("yyyy", -1, date1))
       MsgBox ("q - 季度: " & DateAdd("q", -1, date1))
       MsgBox ("m - 一年中的月份: " & DateAdd("m", -1, date1))
       MsgBox ("y - 一年中的年份: " & DateAdd("y", -1, date1))
       MsgBox ("d - 一年中的一天: " & DateAdd("d", -1, date1))
       MsgBox ("w - 工作日: " & DateAdd("w", -1, date1))
       MsgBox ("ww - 星期: " & DateAdd("ww", -1, date1))
       MsgBox ("h - 小时: " & DateAdd("h", -1, "01-Jan-2013 12:00:00"))
       MsgBox ("n - 分钟: " & DateAdd("n", -1, "01-Jan-2013 12:00:00"))
       MsgBox ("s - 秒钟 : " & DateAdd("s", -1, "01-Jan-2013 12:00:00"))
    End Sub
    

    DateDiff

    学习材料:https://www.yiibai.com/vba/vba_datediff_function.html
    返回两个指定的时间间隔之间的差值。
    丸子:不太明白一年中的年和天是啥意思

    用法

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

    Private Sub Constant_demo_Click()
       Dim fromDate As Variant
       fromDate = "01-Jan-2009 00:00:00"
    
       Dim toDate As Variant
       toDate = "01-Jan-2010 23:59:00"
    
       MsgBox ("yyyy - 年份: " & DateDiff("yyyy", fromDate, toDate))
       MsgBox ("q - 季度: " & DateDiff("q", fromDate, toDate))
       MsgBox ("m - 一年中的月份: " & DateDiff("m", fromDate, toDate))'给的参数里面有俩 m
       MsgBox ("y - 一年中的年份: " & DateDiff("y", fromDate, toDate))
       MsgBox ("d - 一年中的一天:" & DateDiff("d", fromDate, toDate))
       MsgBox ("w - 工作日: " & DateDiff("w", fromDate, toDate))
       MsgBox ("ww - 星期: " & DateDiff("ww", fromDate, toDate))
       MsgBox ("h - 小时: " & DateDiff("h", fromDate, toDate))
       MsgBox ("n - 分钟: " & DateDiff("n", fromDate, toDate))'给的参数里面没有 n
       MsgBox ("s - 秒钟: " & DateDiff("s", fromDate, toDate))
    End Sub
    

    DatePart()

    学习资料:https://www.yiibai.com/vba/vba_datepart_function.html
    返回给定日期的特定部分。

    用法

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

    Private Sub DatePart_demo_Click()
       Dim Quarter As Variant
       Dim DayOfYear As Variant
       Dim WeekOfYear As Variant
    
       Date1 = "2020-02-27"
       Quarter = DatePart("q", Date1)
    
       MsgBox ("q - 季度: " & Quarter)
       DayOfYear = DatePart("y", Date1)
    
       MsgBox ("y - 一年中的年份: " & DayOfYear)
       WeekOfYear = DatePart("ww", Date1)
    
       MsgBox ("ww - 星期: " & WeekOfYear)
       MsgBox ("m - 一年中的月份: " & DatePart("m", Date1))
    End Sub
    

    DateSerial()

    学习资料:https://www.yiibai.com/vba/vba_dateserial_function.html
    返回指定日期、月份和年份参数的日期。
    丸子:分别输入年月日,返回固定格式 YYYY/MM/DD

    用法

    DateSerial(year,month,day)

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

    FormatDateTime()

    学习资料:https://www.yiibai.com/vba/vba_formatdatetime_function.html
    根据提供的参数格式化日期。

    用法

    FormatDateTime(date,format)

    Private Sub FormatDateTimedemo()
       d = ("2020-02-27 12:32")
       MsgBox ("0 = vbGeneralDate - 默认值: " & FormatDateTime(d))
       MsgBox ("1 = vbLongDate - 返回长日期: " & FormatDateTime(d, 1))
       MsgBox ("2 = vbShortDate - 返回短日期: " & FormatDateTime(d, 2))
       MsgBox ("3 = vbLongTime - 返回长时间: " & FormatDateTime(d, 3))
       MsgBox ("4 = vbShortTime - 返回短时间: " & FormatDateTime(d, 4))
    End Sub
    
  • 相关阅读:
    [转]SVN服务器搭建和使用(二)
    [转]SVN服务器搭建和使用(一)
    BZOJ 2049 Sdoi2008 Cave 洞穴勘测
    BZOJ 1589 Usaco2008 Dec Trick or Treat on the Farm 采集糖果
    BZOJ 2796 POI2012 Fibonacci Representation
    BZOJ 2115 Wc2011 Xor
    BZOJ 3105 CQOI2013 新Nim游戏
    BZOJ 2460 Beijing2011 元素
    BZOJ 3687 简单题
    BZOJ 1068 SCOI2008 压缩
  • 原文地址:https://www.cnblogs.com/lisaisacat/p/12371521.html
Copyright © 2011-2022 走看看