VBA/Excel/Access/Word/Data Type/Date Type

Материал из VB Эксперт
Перейти к: навигация, поиск

Create Date type variable

 
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



Date and Time Functions

 
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



Date Constants

 
Sub constSub()
    Const conDate As Date = #3/2/2004#
    Debug.Print conDate
End Sub



Date value calculation

 
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



Times

 
Sub time()
    Dim datTime As Date
    datTime = #3:20:15 AM#
    
    datTime = #3:20:15 AM#
End Sub



Try It Out-Dates

 
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



Variables of type Date must have their values enclosed in # signs

 
Sub dateSub()
    Dim thisDate As Date
    thisDate = #10/8/2003#
    Debug.Print thisDate
End Sub