VBA/Excel/Access/Word/Date Functions/DatePart

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

Break a single date down to its individual components using the DatePart function

 
Sub dateFunctions()
   Dim strDateString As String
   strDateString = "The year part is: " & DatePart("yyyy", Date) & vbCrLf & _
                   "The quarter part is: " & DatePart("q", Now) & vbCrLf & _
                   "The month part is: " & DatePart("m", Now) & vbCrLf & _
                   "The day part is: " & DatePart("d", Now) & vbCrLf & _
                   "The weekday is: " & DatePart("w", Now) & vbCrLf & _
                   "The week part is: " & DatePart("ww", Now) & vbCrLf & _
                   "The hour part is: " & DatePart("h", Now) & vbCrLf & _
                   "The minute part is: " & DatePart("n", Now) & vbCrLf & _
                   "The second part is: " & DatePart("s", Now)
   msgBox strDateString
End Sub



DatePart("d", Now)

 
Sub dateFunctions4()
   Debug.Print "The day part is: " & DatePart("d", Now)
End Sub



DatePart("h", Now)

 
Sub dateFunctions7()
   Debug.Print "The hour part is: " & DatePart("h", Now)
End Sub



DatePart("m", Now)

 
Sub dateFunctions3()
   Debug.Print "The month part is: " & DatePart("m", Now)
End Sub



DatePart("n", Now)

 
Sub dateFunctions8()
   Debug.Print "The minute part is: " & DatePart("n", Now)
End Sub



DatePart("q", Now)

 
Sub dateFunctions2()
   Debug.Print "The quarter part is: " & DatePart("q", Now)
End Sub



DatePart returns "Prints the Year

 
Sub DatePartExample()
    Debug.Print DatePart("YYYY", Now)
    
End Sub



DatePart("s", Now)

 
Sub dateFunctions9()
   Debug.Print "The second part is: " & DatePart("s", Now)
End Sub



DatePart("w", Now)

 
Sub dateFunctions5()
   Debug.Print "The weekday is: " & DatePart("w", Now)
End Sub



DatePart("ww", Now)

 
Sub dateFunctions6()
   Debug.Print "The week part is: " & DatePart("ww", Now)
End Sub



Prints the Day of the Year

 
Sub DatePartExample()
    Debug.Print DatePart("Y", Now)
End Sub



Prints the Month Number

 
Sub DatePartExample()
    Debug.Print DatePart("M", Now)
End Sub



Prints the Quarter Number

 
Sub DatePartExample()
    Debug.Print DatePart("Q", Now)
End Sub



Prints the Week of the Year

 
Sub DatePartExample()
    Debug.Print DatePart("WW", Now)
End Sub



Using DatePart(interval, date[,firstdayofweek[, firstweekofyear]]) to Parse Dates

 
Constant         Value      Year Starts with Week         
vbUseSystem      0          Use the system setting.        
vbFirstJan1      1          The week in which January 1 falls (the default setting).        
vbFirstFourDays  2          The first week with a minimum of four days in the year.        
vbFirstFullWeek  3          The first full week (7 days) of the year.      

the following statement assigns the current year to the variable dteThisYear:
Sub datePart()
    Debug.Print DatePart("yyyy", Date)
End Sub