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

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

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

   <source lang="vb">

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

</source>
   
  


DatePart("d", Now)

   <source lang="vb">

Sub dateFunctions4()

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

End Sub

</source>
   
  


DatePart("h", Now)

   <source lang="vb">

Sub dateFunctions7()

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

End Sub

</source>
   
  


DatePart("m", Now)

   <source lang="vb">

Sub dateFunctions3()

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

End Sub

</source>
   
  


DatePart("n", Now)

   <source lang="vb">

Sub dateFunctions8()

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

End Sub

</source>
   
  


DatePart("q", Now)

   <source lang="vb">

Sub dateFunctions2()

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

End Sub

</source>
   
  


DatePart returns "Prints the Year

   <source lang="vb">

Sub DatePartExample()

   Debug.Print DatePart("YYYY", Now)
   

End Sub

</source>
   
  


DatePart("s", Now)

   <source lang="vb">

Sub dateFunctions9()

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

End Sub

</source>
   
  


DatePart("w", Now)

   <source lang="vb">

Sub dateFunctions5()

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

End Sub

</source>
   
  


DatePart("ww", Now)

   <source lang="vb">

Sub dateFunctions6()

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

End Sub

</source>
   
  


Prints the Day of the Year

   <source lang="vb">

Sub DatePartExample()

   Debug.Print DatePart("Y", Now)

End Sub

</source>
   
  


Prints the Month Number

   <source lang="vb">

Sub DatePartExample()

   Debug.Print DatePart("M", Now)

End Sub

</source>
   
  


Prints the Quarter Number

   <source lang="vb">

Sub DatePartExample()

   Debug.Print DatePart("Q", Now)

End Sub

</source>
   
  


Prints the Week of the Year

   <source lang="vb">

Sub DatePartExample()

   Debug.Print DatePart("WW", Now)

End Sub

</source>
   
  


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

   <source lang="vb">

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

</source>