VBA study notes - Date

date()

Returns the current system date
is returned in the format YYYY / MM / DD

CDate()

Learning materials: https://www.yiibai.com/vba/vba_cdate_function.html
the valid date and time expression to type Date.

usage

cdate (date)
balls: the input is converted to a fixed date format: YYYY / MM / DD
Support "on the day", "date" format, which can be a month abbreviation, but Libre Office editor does not support this formats, will complain, can only use the Micro Office Excel program comes with development tools.

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()

Learning materials: https://www.yiibai.com/vba/vba_dateadd_function.html
return a specified time interval date is added.

usage

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

Learning materials: https://www.yiibai.com/vba/vba_datediff_function.html
returns the difference between two specified time interval.
Balls: do not understand the years and the days of the year What do you mean

usage

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()

Learning materials: https://www.yiibai.com/vba/vba_datepart_function.html
return to a specific part of a given date.

usage

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()

Learning materials: https://www.yiibai.com/vba/vba_dateserial_function.html
return date specified date, month and year parameters.
Balls: respectively, enter the date, returns a fixed format YYYY / MM / DD

usage

DateSerial(year,month,day)

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

FormatDateTime()

Learning materials: https://www.yiibai.com/vba/vba_formatdatetime_function.html
parameters to format dates provided by.

usage

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

Guess you like

Origin www.cnblogs.com/lisaisacat/p/12371521.html