DATEDIFF(interval, date1, date2 [,firstdayofweek] [,firstweekofyear])

Returns the number of a given time interval between two specified dates (Long).


intervalThe interval of time you want to calculate (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
date1The start date (Date).
date2The finish date (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 (default)
2 = vbFirstFourDays
3 = vbFirstFullWeek

REMARKS
* If "interval" = "w", the number of weeks is based on how many days. 7 days = 1 week.
* If "interval" = "ww", the number of weeks is based on how many Sundays there are.
* If "interval" = "ww" and "date1" falls on a Sunday, it will not get included/counted.
* If "interval" = "ww" and "date2" falls on a Sunday, it will get included/counted.
* If "date1" or "date2" are enclosed in double quotation marks and the year is not entered then the current year is used.
* If "date1" > "date2" then a negative number is returned.
* 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.
* The "firstdayofweek" is only relevant if the "interval" is either "w" or "ww".
* This function can be used to determine how many specified time intervals exist between two dates.
* You can use the DATE 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 DATEPART function to return the specified part of a given date.
* You can use the DATESERIAL function to return the date given a year, month and day.
* The equivalent Excel function is Application.WorksheetFunction.DATEDIF
* The equivalent .NET function is Microsoft.VisualBasic.DateAndTime.DateDiff
* For the Microsoft documentation refer to docs.microsoft.com

'Return number of days between
Debug.Print DateDiff("d", "1 Jan 2022", "1 Apr 2022") '= 90
Debug.Print DateDiff("y", "1 Jan 2022", "1 Apr 2022") '= 90

'Return number of weeks (based on 7 days)
Debug.Print DateDiff("w", "1 Jan 2022", "2 Jan 2022") '= 0
Debug.Print DateDiff("w", "1 Jan 2022", "3 Jan 2022") '= 0
Debug.Print DateDiff("w", "1 Jan 2022", "7 Jan 2022") '= 0
Debug.Print DateDiff("w", "1 Jan 2022", "8 Jan 2022") '= 1
Debug.Print DateDiff("w", "1 Jan 2022", "14 Jan 2022") '= 1
Debug.Print DateDiff("w", "1 Jan 2022", "15 Jan 2022") '= 2

Debug.Print DateDiff("w", "4 Jan 2022", "7 Jan 2022") '= 0
Debug.Print DateDiff("w", "4 Jan 2022", "10 Jan 2022") '= 0
Debug.Print DateDiff("w", "4 Jan 2022", "11 Jan 2022") '= 1
Debug.Print DateDiff("w", "4 Jan 2022", "17 Jan 2022") '= 1
Debug.Print DateDiff("w", "4 Jan 2022", "18 Jan 2022") '= 2

'Return number of weeks (based on the specific weekday)
'1 Jan 2022 = Saturday
'2 Jan 2022 = Sunday
'3 Jan 2022 = Monday
Debug.Print DateDiff("ww", "1 Jan 2022", "2 Jan 2022") '= 1
Debug.Print DateDiff("ww", "1 Jan 2022", "3 Jan 2022") '= 1
Debug.Print DateDiff("ww", "1 Jan 2022", "7 Jan 2022") '= 1
Debug.Print DateDiff("ww", "1 Jan 2022", "8 Jan 2022") '= 1
Debug.Print DateDiff("ww", "1 Jan 2022", "14 Jan 2022") '= 2
Debug.Print DateDiff("ww", "1 Jan 2022", "15 Jan 2022") '= 2

Debug.Print DateDiff("ww", "4 Jan 2022", "7 Jan 2022") '= 0
Debug.Print DateDiff("ww", "4 Jan 2022", "10 Jan 2022") '= 1
Debug.Print DateDiff("ww", "4 Jan 2022", "11 Jan 2022") '= 1
Debug.Print DateDiff("ww", "4 Jan 2022", "17 Jan 2022") '= 2
Debug.Print DateDiff("ww", "4 Jan 2022", "18 Jan 2022") '= 2

Debug.Print DateDiff("d", VBA.Date(), "31/12/2021", 3, 0) '= 117
Debug.Print DateDiff("d", VBA.Date(), "31/12/2004", 2, 0) '= -6092
Debug.Print DateDiff("yyyy","31 Dec 2003", "1 Jan 2004") '= 1
Debug.Print DateDiff("d", #12/1/2001#, #12/1/2000#, 0, 0) '= -365 - 2000 is a leap year so Feb has 28 days

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