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 DATEDADD 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-2008") = 1 
Debug.Print DatePart("d", "10-01-2008") = 10
Debug.Print DatePart("w", "10-01-2008") = 5 this is the weekday of the date
Debug.Print DatePart("ww", "10-01-2008") = 2
Debug.Print DatePart("m", "01-10-2008") = 10
Debug.Print DatePart("y", Now()) = 298
Debug.Print DatePart("h", "01-01-2008 12:00:00") = 12
Debug.Print DatePart("n", "01-01-2008 00:12:00") = 12
Debug.Print DatePart("s", "01-01-2008 00:00:30") = 30

Dim dtDate As Date
Dim iReturn As Integer

dtDate = #7/20/2007 12:10:56 PM#
iReturn = DatePart("yyyy", dtDate)
Debug.Print iReturn '2007

iReturn = DatePart("q", dtDate)
Debug.Print iReturn '3

iReturn = DatePart("m", dtDate)
Debug.Print iReturn '7

iReturn = DatePart("y", dtDate)
Debug.Print iReturn '201

iReturn = DatePart("d", dtDate)
Debug.Print iReturn '20

iReturn = DatePart("w", dtDate)
Debug.Print iReturn '6

iReturn = DatePart("ww", dtDate)
Debug.Print iReturn '29

iReturn = DatePart("h", dtDate)
Debug.Print iReturn '12

iReturn = DatePart("n", dtDate)
Debug.Print iReturn '10

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

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