VBA/Excel/Access/Word/Data Type/Date Type
Содержание
Create Date type variable
<source lang="vb">
Sub DateTest()
Dim datDec As Date Dim datMay As Date datMay = #5/1/1998# datDec = #12/1/1998# Debug.Print DatePart("m", datDec) Debug.Print DateDiff("m", datMay, datDec) Debug.Print "August is " & DateAdd("m", 3, datMay)
End Sub
</source>
Date and Time Functions
<source lang="vb">
Sub dateFunctions()
Dim strDateString As String strDateString = "The present date and time is: " & Now & vbCrLf & _ "Today"s date is: " & Date & vbCrLf & _ "The day of the month is: " & Day(Date) & vbCrLf & _ "The day of the week is: " & Weekday(Date) & vbCrLf & _ "The name of the week day is: " & WeekdayName(Weekday(Date)) & vbCrLf & _ "The weekday abbreviated is: " & WeekdayName(Weekday(Date), True) & vbCrLf & _ "The month is: " & Month(Date) & vbCrLf & _ "The name of the month is: " & MonthName(Month(Date)) & vbCrLf & _ "The month abbreviated is: " & MonthName(Month(Date), True) & vbCrLf & _ "The year is: " & Year(Date) msgBox strDateString
End Sub
</source>
Date Constants
<source lang="vb">
Sub constSub()
Const conDate As Date = #3/2/2004# Debug.Print conDate
End Sub
</source>
Date value calculation
<source lang="vb">
Sub dateDemo()
Dim datToday As Date Dim datNextWeek As Date Dim datLastWeek As Date Dim datePaymentDate As Date datToday = Date datNextWeek = datToday + 7 datLastWeek = datToday - 7 datPaymentDate = datToday + 30
End Sub
</source>
Times
<source lang="vb">
Sub time()
Dim datTime As Date datTime = #3:20:15 AM# datTime = #3:20:15 AM#
End Sub
</source>
Try It Out-Dates
<source lang="vb">
Sub DateTest()
Dim datDec As Date Dim datMay As Date datMay = #5/1/1998# datDec = #12/1/1998# Debug.Print DatePart("m", datDec) Debug.Print DateDiff("m", datMay, datDec) Debug.Print "August is " & DateAdd("m", 3, datMay)
End Sub
</source>
Variables of type Date must have their values enclosed in # signs
<source lang="vb">
Sub dateSub()
Dim thisDate As Date thisDate = #10/8/2003# Debug.Print thisDate
End Sub
</source>