DATEPART(interval, date [,firstdayofweek] [,firstweekofyear])

Returns the specified part of a given date (Integer).


intervalThe interval of time you want to add (String):
"yyyy","YYYY" = year
"q","Q" = quarter
"m","M" = month
"y","Y" = day of year
"d","D" = day
"w","W" = weekday
"ww","WW" = week
"h","H" = hour
"n","N" = minute
"s","S" = second
dateThe date you want to evaluate (Date).
firstdayofweek(Optional) A vbDayOfWeek constant that specifies the first day of the week:
0 = vbUseSystemDayofWeek
1 = vbSunday (default)
2 = vbMonday
3 = vbTuesday
4 = vbWednesday
5 = vbThursday
6 = vbFriday
7 = vbSaturday
firstweekofyear(Optional) A vbFirstWeekOfYear constant that specifies the first week of the year:
0 = vbUseSystem
1 = vbFirstJan1, the week containing Jan 1st (default)
2 = vbFirstFourDays, the first week containing 4 days
3 = vbFirstFullWeek, the first full week

REMARKS
* If "firstdayofweek" is left blank, then 1 is used (ie Sunday).
* If "firstweekofyear" is left blank, then the first week is assumed to be the week in which January 1 occurs.
* In the case of years , the calculation starts from year 0. but in the case of quarters, months, weeks, weeks of year, day of year it starts from the first day of the current year.
* The "firstdayofweek" is only relevant if the "interval" is either "w" or "ww".
* This function can be used to evaluate a date and return a specific interval of time.
* This function can be used to calculate the day of the week or the current hour.
* If the date is submitted as a string and does not specify a year (for example #20 July#) then the current year is automatically inserted.
* If you are calling this function from VB.Net then the following enumerations can be used: VisualBasic.DateInterval, VisualBasic.FirstDayOfWeek, VisualBasic.FirstWeekOfYear
* You can use the DATE - Function function to return the current system date.
* You can use the DATEADD function to return the date with a specified time interval added.
* You can use the DATEDIFF function to return the number of a given time interval between two specified dates.
* The equivalent .NET function is Microsoft.VisualBasic.DateAndTime.DatePart
* For the Microsoft documentation refer to docs.microsoft.com

Debug.Print DatePart("d", "01-01-2020")          '= 1  
Debug.Print DatePart("d", "10-01-2020") '= 10
Debug.Print DatePart("w", "10-01-2020") '= 6 this is the weekday of the date
Debug.Print DatePart("ww", "10-01-2020") '= 2
Debug.Print DatePart("m", "01-10-2020") '= 10
Debug.Print DatePart("y", Now()) '= 248
Debug.Print DatePart("h", "01-01-2020 12:00:00") '= 12
Debug.Print DatePart("n", "01-01-2020 00:12:00") '= 12
Debug.Print DatePart("s", "01-01-2020 00:00:30") '= 30

Dim dtDate As Date
dtDate = #7/20/2020 12:10:56 PM#
Debug.Print DatePart("yyyy", dtDate) '= 2020
Debug.Print DatePart("q", dtDate) '= 3
Debug.Print DatePart("m", dtDate) '= 7
Debug.Print DatePart("y", dtDate) '= 202
Debug.Print DatePart("d", dtDate) '= 20
Debug.Print DatePart("w", dtDate) '= 2
Debug.Print DatePart("ww", dtDate) '= 30
Debug.Print DatePart("h", dtDate) '= 12
Debug.Print DatePart("n", dtDate) '= 10

Dim iReturn As Integer
iReturn = DatePart("s", dtDate)
Debug.Print iReturn '= 56

© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited Top