Date Operations
The date operations are shown in the following table.
- Add or subtract a duration in years, months, days, hours, minutes, seconds, or microseconds
- Determine the duration between two dates, times, or timestamps
- Extract a portion of a date, time, or timestamp (for example, the day)
- Test that a value is valid as a date, time, or timestamp.
To add or subtract a duration, you can use the + or - operator in free-form syntax or the ADDDUR or SUBDUR operation code in traditional syntax. The following table shows the built-in functions that you use in free-form syntax and the duration codes that you use in traditional syntax.
Unit | Built-In Function | Duration Code |
---|---|---|
Year | %YEARS | *YEARS or *Y |
Month | %MONTHS | *MONTHS or *M |
Day | %DAYS | *DAYS or *D |
Hour | %HOURS | *HOURS or *H |
Minute | %MINUTES | *MINUTES or *MN |
Second | %SECONDS | *SECONDS or *S |
Microsecond | %MSECONDS | *MSECONDS or *MS |
C ADDDUR 23:*D DUEDATE
/FREE
newdate = duedate + %DAYS(23)
/END-FREE
To calculate the duration between two dates, times, or timestamps, you can use the %DIFF built-in function in free-form syntax or the SUBDUR operation code in traditional syntax. In either case, you must specify one of the duration codes shown in Table 2.
The duration is given in complete units, with any remainder discarded. A duration of 59 minutes, expressed in hours, is 0. A duration of 61 minutes, expressed in hours, is 1.
The following table shows additional examples, using the SUBDUR operation code. The %DIFF built-in function would give the same results.
Duration Unit | Factor 1 | Factor 2 | Result |
---|---|---|---|
Months | 1999-03-28 | 1999-02-28 | 1 month |
1999-03-14 | 1998-03-15 | 11 months | |
1999-03-15 | 1998-03-15 | 12 months | |
Years | 1999-03-14 | 1998-03-15 | 0 years |
1999-03-15 | 1998-03-15 | 1 year | |
1999-03-14-12.34.45.123456 | 1998-03-14-12.34.45.123457 | 0 years | |
Hours | 1990-03-14-23.00.00.000000 | 1990-03-14-22.00.00.000001 | 0 hours |