Caché Basic Reference
DateDiff


Returns the number of intervals between two dates.
Synopsis
DateDiff(interval,date1,date2[,firstdayofweek[,firstweekofyear]])
Arguments
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:
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:
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 nonleap 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 nonleap 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