Caché Basic Reference
DateDiff
[Back] [Next]
   
Server:docs2
Instance:LATEST
User:UnknownUser
 
-
Go to:
Search:    

Returns the number of intervals between two dates.
Synopsis
DateDiff(interval,date1,date2[,firstdayofweek[,firstweekofyear]])
Arguments
interval A string expression code that specifies the interval you want to use to calculate the difference between date1 and date2. Specified as a quoted string. A list of these interval codes is provided below.
date1, date2 Two date expressions. The two dates you want to use in the calculation. These date expressions can optionally also include a time component. If the time is omitted, it defaults to 00:00:00.
firstdayofweek Optional — Constant that specifies the day of the week. If not specified, Sunday is assumed. A list of the available constants is provided below.
firstweekofyear Optional — Constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs. A list of the available constants is provided below.
Description
You can use the DateDiff function to determine how many specified time intervals exist between two dates. For example, you might use DateDiff to calculate the number of days between two dates, or the number of weeks between today and the end of the year. DateDiff returns a positive integer for the number of intervals if date1 is earlier than date2; otherwise it returns a negative integer for the number of intervals. If both dates are the same, or if the time between them is less than the specified interval, DateDiff returns zero (0).
Intervals are calculated from the specified unit itself. Thus a year interval is determined by whether the two year dates differ, not by how many days have elapsed. Similarly, a day interval is determined by whether the two dates differ, not by how many hours have elapsed.
The interval argument can have the following values:
Setting Description
yyyy Year
q Quarter
m Month
y Day of Year
d Day
w Weekday (number of seven-day units)
ww Week (number of calendar weeks)
h Hour
n Minute
s Second
To calculate the number of days between date1 and date2, you can use either Day ("d") or Day of Year ("y").
To calculate the number of weeks between date1 and date2 you can use Weekday ("w") or Week ("ww"). When interval is Weekday ("w"), DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, DateDiff counts the number of Mondays until date2. It counts date2 but not date1. If interval is Week ("ww"), however, the DateDiff function returns the number of calendar weeks between the two dates. It counts the number of Sundays between date1 and date2. DateDiff counts date2 if it falls on a Sunday; but it does not count date1, even if it does fall on a Sunday.
The firstdayofweek argument affects calculations that use the "w" and "ww" interval symbols. The firstdayofweek argument can have the following values:
Constant Value Description
vbUseSystem 0 Use National Language Support (NLS) API setting.
vbSunday 1 Sunday
vbMonday 2 Monday
vbTuesday 3 Tuesday
vbWednesday 4 Wednesday
vbThursday 5 Thursday
vbFriday 6 Friday
vbSaturday 7 Saturday
The firstweekofyear argument can have the following values:
Constant Value Description
vbUseSystem 0 Use National Language Support (NLS) API setting.
vbFirstJan1 1 Use the week in which January 1 occurs (default).
bFirstFourDays 2 Use the first week that has at least four days in the new year.
vbFirstFullWeek 3 Use the first full week of the year.
The Year ("yyyy") interval calculates number of years based on the year date, not the number of elapsed days. Thus, when comparing December 31 to January 1 of the immediately succeeding year, DateDiff for Year ("yyyy") returns 1, even though only a day has elapsed.
If date1 or date2 is a date literal, the specified year becomes a permanent part of that date. However, if date1 or date2 is enclosed in quotation marks (" ") and you omit the year, the current year is inserted in your code each time the date1 or date2 expression is evaluated. This makes it possible to write code that can be used in different years.
Examples
The following example uses the DateDiff function to display the number of days between a given date and today:
DiffADate =  DateDiff("d","11/12/1953",Date)
Print "Days to the present day: "
Println DiffADate
 
The following example calculates the number of each date interval unit between November 12, 1953 and November 1, 2005:
NewDay = DateDiff("d","11/12/1953","11/1/2005")
NewWeek = DateDiff("w","11/12/1953","11/1/2005")
NewMonth = DateDiff("m","11/12/1953","11/1/2005")
NewQuarter = DateDiff("q","11/12/1953","11/1/2005")
NewYDay = DateDiff("y","11/12/1953","11/1/2005")
NewYear = DateDiff("yyyy","11/12/1953","11/1/2005")
Println NewDay
Println NewWeek
Println NewMonth
Println NewQuarter
Println NewYDay
Println NewYear
 
The following example calculates the number of each days between January 1 and March 1 on a leap year (2004) and a non-leap year (2005):
LeapDays = DateDiff("d","1/1/2004","3/1/2004")
NLeapDays = DateDiff("d","1/1/2005","3/1/2005")
Println LeapDays
Println NLeapDays
 
As one would expect, the difference is 60 days in leap years, and 59 days in non-leap years.
The following example calculates the number of time intervals between two successive days. Note that if the time is not specified, it defaults to 00:00:00:
NumH = DateDiff("h","1/1/2004","1/2/2004")
NumHNoon = DateDiff("h","1/1/2004","1/2/2004 12:00:00")
NumMin = DateDiff("n","1/1/2004","1/2/2004")
NumMinNoon = DateDiff("n","1/1/2004","1/2/2004 12:00:00")
NumSec = DateDiff("s","1/1/2004","1/2/2004")
NumSecNoon = DateDiff("s","1/1/2004","1/2/2004 12:00:00")
Println NumH
Println NumHNoon
Println NumMin
Println NumMinNoon
Println NumSec
Println NumSecNoon
 
See Also