Arithmetic With Date and Time Values

To perform arithmetic with date and time values, Uniface converts these values internally to the equivalent number of days.

For example, one second is 1/86,400 (1.1574 * 10 -5) days. Using a common unit of measurement for all parts of the whole makes it possible to use the full range of operations (that is, addition, subtraction, multiplication, and division) to perform arithmetic with date and time values.

Normalization of Date and Time Values

Uniface automatically normalizes all expressions used to the internal equivalent in numbers of days since 1 January 0000 00:00:00.00. This has the following very important implications:

  • You do not need to worry about which units of date or time to use when writing Procs because (for example) the expression '8640000t' (8,640,000 ticks) is the same as '1d' (one day): Uniface treats both these expressions as one day, because there are 8,640,000 ticks in a day.
  • The results of some calculations might be confusing, particularly when doing either of the following:
    • Calculating the elapsed time between two dates.
    • Expressing the result in months.

For example, although the elapsed time between 1 March 1990 and 1 May 1990 is two months, Uniface would return an elapsed time of two months and one day, because the difference is 61 days which, in year 0 (year 0 is a leap year), takes us to 1 March.

Giving years and months a linear display format can have strange effects, because Uniface uses the same rules to work out the linear values as it uses for nonlinear Datetimes; that is, all values use the base date of 1 January 0000.

Leap Years

Remember that year 0 is a leap year, so year 0 has 366 days and year 1 has 365 days. Year 2 means a total of 731 days, therefore. If the difference between two dates is more than one year, the first year in the counter stands for 366 days and not 365, as you might expect.

In Uniface, dates are represented in the format ccyymmddhhmmtt. When performing calculations with elapsed time, you need to take this into account. For example, if you subtract a past date 21-may-2007 from a later date 21-may-2008, a difference of one year, the result is 366 days. In the ccyymmddhhmmtt format, this is expressed as 00001231000000, not as 00010000000000 as you might expect. For an example, see Calculating Age.

Some Months are More Equal Than Others

Month 1 has 31 days, because month 1 is January. If you express an elapsed time as the number of months, Uniface counts off 31 days for the first month, even though your elapsed time might be the difference between 3-JUL-1990 and 4-SEP-1990.

Similarly, the second month is February, which usually comprises 28 days, so Uniface counts off 28 days for the second month, even if your elapsed time is the difference between two dates in the middle of the year. However, if the year is a leap year (and year 0 is a leap year), February has 29 days, so Uniface counts off 29 instead of 28 days for the second month; month 2 in this case means a total of 60 days, therefore (31 + 29).

The elapsed time between 3-JUL-1990 and 4-SEP-1990 is an arithmetic operation which results in three days, two months, and zero years. The actual number of days between these two dates is 63, which means one month of January (31 days), plus one month of February in a leap year (29 days), because there are zero years, and year 0 is a leap year, plus three days.

Where possible, avoid the use of months in linear display format definitions. The number of days is usually sufficient.

Linear Date Values

In the examples in the table , LDATE is a Date field with the display format DIS(ld.m.y). The general variables $1 to $5 contain various Date values. The table shows values in the LDATE field as a result of subtracting one general variable from another.

Examples for Date field LDATE with display format DIS(ld.m.y)
Statement Result
$1 = $date("1 feb 19")  
$2 = $date("1 mar 19")  
$3 = $date("1 apr 19")  
$4 = $date("1 may 19")  
$5 = $date("1 jan 18")  
LDATE = $2 - $1

LDATE contains 28.0.0 (28 days)

LDATE = $3 - $2

LDATE contains 0.1.0 (31 days)

LDATE = $4 - $2

LDATE contains 1.2.0 (61 days)

LDATE = $1 - $5

LDATE contains 0.1.1 (397 days)

LDATE = $1 + 5

LDATE contains 6.1.1989

Date and Time Arithmetic

The examples in the table show how to do arithmetic operations with Date and Time values:

Examples of Arithmetic with Date and Time Values

Statement

Result

; DATE_FLD is Date

; DATM_FLD is Datetime

; END_TIME is Datetime

; START_TIME is Datetime

; TIME_FLD is Time

$1 = END_TIME - START_TIME

Elapsed time.

$2 = DATE_FLD

Get a Date value.

$3 = TIME_FLD

Get a Time value.

$4 = $2 + $3

Make combined Datetime.

$5 = $4 + $1

Add elapsed time from $1 to $4.

$5 = $5 + 1s

Add 1 second to Datetime in $5.

$5 = $5 + 1n

Add 1 minute.

$5 = $5 + 1n1s

Add 1 minute and 1 second.

DATE_FLD = DATE_FLD + 4

Add 4 days.

DATM_FLD = DATM_FLD + 12h

Add half a day (12 hours).

$1 = DATM_FLD + 7d12h

Add one week and half a day.

Note: Assigning a negative value to a Linear Date, Linear Time, or Linear Datetime value results in an incorrectly displayed value. If you are evaluating an expression that can result in a negative Linear value, assign it to a Numeric field, not a Linear field.

Related Topics