Clinical Quality Language Release 1 STU2 (w/ Errata) (1.2.1)

This is Clinical Quality Language STU2.1 (1.2 Published w/ Errata) in it's permanent home (it will always be available at this URL). It has been superseded by 1.4.
For a full list of available versions, see the Directory of published versions .

Clinical Decision Support Work GroupMaturity Level: 4Ballot Status: STU 2.1

Appendix H – Time Interval Calculation Examples

To determine the length of time between two dates, CQL provides two different approaches, duration the number of whole periods between two dates, and difference, the number of period boundaries crossed between two dates.

The first approach, calculating the duration, determines the number of whole periods that occur between the two dates. Conceptually, the calculation is performed by considering the two dates on a timeline, and counting the number of whole periods that fit on that timeline between the two dates. For example:

Date 1: 2012-03-10
Date 2: 2013-03-10
Duration In Years: years between Date1 and Date2

The Duration In Years expression gives one year, because an entire year has passed between the two dates. Note that time is considered for the purposes of calculating the number of years:

DateTime 1: 2012-03-10 10:20:00
DateTime 2: 2013-03-10 09:20:00
Duration in Years: years between DateTime1 and DateTime2

This expression gives zero years, because the year has not passed until 10:20:00 on the day in the following year. To calculate the number of years, ignoring the time, extract the date from the date/time value:

DateTime 1: 2012-03-10 10:20:00
DateTime 2: 2013-03-10 09:20:00
Duration In Years: years between (date from DateTime1) and (date from DateTime2)

The second approach, calculating the difference, determines the number of boundaries crossed between two dates. To illustrate the difference, consider the following example:

Date 1: 2012-12-31
Date 2: 2013-01-01
Duration In Years: years between Date1 and Date2
Difference In Years: difference in years between Date1 and Date2

The Duration In Years expression returns zero because a full year has not passed between the two dates. However, the Difference In Years expression returns 1 because one year boundary was crossed between the two dates.

1. Calculating Duration in Years

1.1. Definition

In CQL, a year is defined as the duration of any time interval which starts at a certain time of day at a certain calendar date of the calendar year and ends at:

  • The same time of day on the same calendar date of the next calendar year, if it exists

  • The same time of day on the immediately following calendar date of the next calendar year, if the same calendar date of the next calendar year does not exist.

Note: When in the next calendar year the same calendar date does not exist, the ISO states that the ending calendar day has to be agreed upon. The above convention is used in CQL as a resolution to this issue.

1.2. Examples

  1. Month (date 2) < month (date 1): Duration (years) = year (date 2) - year (date 1) - 1

Example 1:
Date 1: 2012-03-10 22:05:09
Date 2: 2013-02-18 19:10:03
Duration = year (date 2) - year (date 1) - 1 = 2013 - 2012 - 1 = 0 years

  1. Month (date 2) = month (date 1) and day (date 2) >= day (date 1)
    Duration (years) = year (date 2) - year (date 1)

Example 2.a: day (date 1) = day (date 2)
Date 1: 2012-03-10 22:05:09
Date 2: 2013-03-10 22:05:09
Duration = year (date 2) - year (date 1) = 2013 - 2012 = 1 year

Note: Time of day is important in this calculation. If the time of day of Date 2 were less than the time of day for Date 1, the duration of the time interval would be 0 years according to the definition.

Example 2.b: day (date 2) > day (date 1)
Date 1: 2012-03-10 22:05:09
Date 2: 2013-03-20 04:01:30
Duration = year (date 2) - year (date 1) = 2013 - 2012 = 1 year

  1. Month (date 2) = month (date 1) and day (date 2) < day (date 1)
    Duration (years) = year (date 2) - year (date 1) - 1

Example 3.a:
Date 1: 2012-02-29
Date 2: 2014-02-28
Duration = year (date 2) - year (date 1) - 1 = 2014 - 2012 - 1 = 1 year

  1. Month (date 2) > month (date 1)
    Duration (years) = year (date 2) - year (date 1)

Example 4.a:
Date 1: 2012-03-10 11:16:02
Date 2: 2013-08-15 21:34:16
Duration = year (date 2) - year (date 1) = 2013 - 2012 - 1 year

Example 4.b:
Date 1: 2012-02-29 10:18:56
Date 2: 2014-03-01 19:02:34
Duration = year (date 2) - year (date 1) = 2014 - 2012 = 2 years

Note: Because there is no February 29 in 2014, the number of years can only change when the date reaches March 1, the first date in 2014 that surpasses the month and day of date 1 (Feburary 29).

2. Calculating Duration in Months

2.1. Definition

A month in CQL is defines as the duration of any time interval which starts at a certain time of day at a certain calendar day of the calendar month and ends at:

  • The same time of day at the same calendar day of the ending calendar month, if it exists

  • The same time of day at the immediately following calendar date of the ending calendar month, if the same calendar date of the ending month in the ending year does not exist.

Notes: When in the next calendar year the same calendar date does not exist, the ISO states that the ending calendar day has to be agreed upon. The above convention is used in CQL as a resolution to this issue.

2.2. Examples

  1. Day (date 2) >= day (date 1)
    Duration (months) = (year (date 2) - year (date 1)) * 12 + (month (date 2) - month (date 1))

Example 1.a:
Date 1: 2012-03-01 14:05:45
Date 2: 2012-03-31 23:01:49
Duration = (year (date 2) - year (date 1)) * 12 + (month (date 2) - (month (date 1))
= (2012 - 2012) * 12 + (3 - 3) = 0 months

Example 1.b:
Date 1: 2012-03-10 22:05:09
Date 2: 2013-06-30 13:00:23
Duration = (year (date 2) - year (date 1)) * 12 + (month (date 2) - (month date 1))
= (2013 - 2012) * 12 + (6 - 3) = 12 + 3 = 15 months

  1. Day (day 2) < day (date 1)
    Duration (months) = (year (date 2) - year (date 1)) * 12 + (month (date 2) - month (date 1)) - 1

Example 2:
Date 1: 2012-03-10 22:05:09
Date 2: 2013-01-09 07:19:33
Duration = (year (date 2) - year (date 1)) * 12 + (month (date 2) - month (date 1)) - 1
= (2013 - 2012) * 12 + (1 - 3) - 1 = 12 - 2 - 1 = 9 months

3. Calculating Duration in Weeks

3.1. Definition

In CQL, a week is defined as a duration of any time interval which starts at a certain time of day at a certain calendar day at a certain calendar week and ends at the same time of day at the same calendar day of the ending calendar week. In other words, a complete week is always seven days long.

3.2. Examples

  1. Duration = [date 2 - date 1 (days)] / 7

Example 1:
Date 1: 2012-03-10 22:05:09
Date 2: 2012-03-20 07:19:33
Duration = [# days (month (date 1)) - day (date 1) + # days (month (date 1) + 1) + #days (month (date 1) + 2) + …​ + # days (month (date 2) - 1) + day (date 2)] / 7
= (20 - 10) / 7 = 10 / 7 = 1 week

4. Calculating Duration in Days

4.1. Definition

In CQL, a day is defined as a duration of any time interval which starts at a certain calendar day and ends at the next calendar day (1 second to 23 hours, 59 minutes, and 59 seconds).

The duration in days between two dates will generally be given by subtracting the start calendar date from the end calendar date, respecting the time of day between the two dates.

4.2. Examples

  1. Time (date 2) < time (date 1)
    Duration = [date 2 - date 1 (days)] - 1

Example 1:
Date 1: 2012-01-31 12:30:00
Date 2: 2012-02-01 09:00:00
Duration = 02-01 - 01-31 - 1 = 0 days

  1. Time (date 2) >= time (date 1)
    Duration = date 2 - date 1 (days)

Example 2:
Date 1: 2012-01-31 12:30:00
Date 2: 2012-02-01 14:00:00
Duration = 02-01 - 01-31 = 1 day

5. Calculating Duration in Hours

5.1. Definition

In CQL, an hour is defined as 60 minutes. The duration in hours between two dates is the number of minutes between the two dates, divided by 60. The result is truncated to the unit.

5.2. Examples

  1. Example 1:
    Date 1: 2012-03-01 03:10:00
    Date 2: 2012-03-01 05:09:00
    Duration = 1 hour

  2. Example 2:
    Date 1: 2012-02-29 23:10:00
    Date 2: 2012-03-01 00:10:00
    Duration = 1 hour

  3. Example 3:
    Date 1: 2012-03-01 03:10
    Date 2: 2012-03-01 04:00
    Duration = 0 hours

6. Calculating Duration in Minutes

6.1. Definition

In CQL, a minute is defined as 60 seconds. The duration in minutes between two dates is the number of seconds between the two dates, divided by 60. The result is truncated to the unit.

6.2. Examples

  1. Example 1:
    Date 1: 2012-03-01 03:10:00
    Date 2: 2012-03-01 05:20:00
    Duration = 130 minutes

  2. Example 2:
    Date 1: 2012-02-29 23:10:00
    Date 2: 2012-03-01 00:20:00
    Duration = 70 minutes

7. Difference Calculations

Difference calculations are performed by truncating the date/time values at the next precision, and then performing the corresponding duration calculation on the truncated values.

7.1. Examples

  1. Example 1:
    Date 1: 2012-03-01 03:10:00
    Date 2: 2012-12-31 10:10:00
    Difference (years) = Duration (years) between 2012-01-01 00:00:00 and 2012-01-01 00:00:00
    Difference (years) = 0

  2. Example 2:
    Date 1: 2012-12-31 03:10:00
    Date 2: 2013-01-01 10:10:00
    Difference (years) = Duration (years) between 2012-01-01 00:00:00 and 2013-01-01 00:00:00
    Difference (years) = 1

  3. Example 3:
    Date 1: 2016-10-10 09:00:00
    Date 2: 2016-10-11 11:59:00
    Difference (days) = Duration (days) between 2016-10-10 00:00:00 and 2016-10-11 00:00:00
    Difference (days) = 1

  4. Example 4:
    Date 1: 2016-10-10 09:00:00
    Date 2: 2016-10-12 00:00:00
    Difference (days) = Duration (days) between 2016-10-10 00:00:00 and 2016-10-12 00:00:00
    Difference (days) = 2