datdif

Syntax

days = DATDIF(date1, date2)

Arguments

days: integer

date1, date2: text

Meaning

Parameters date1 and date2 must be valid dates in the Julian, European or ISO format (ISO dates must be complete dates, not just a year or a year and a month*). The American format is not supported (see the description of the DATE$-function). This function gives the number of days between the dates date1 and date2. If date2 is later than date1, the result will be positive; if date2 is earlier than date1, the result will be negative. For both dates, you can also use 'today' or date$(<code>). The system will then automatically fill in the value that is valid when the adapl is carried out. It is also possible to combine 'today' or date$(<code>) with a plus or a minus sign followed by an integer (e.g. 'today' -10 ). That signifies the system date plus or minus the stated number of days.
* To compare incomplete ISO dates, you could use the adapl code from the second example below in which we replace the first '-' in the date strings after the year (taking into account that the date may start with a '-', meant as minus symbol) by a '.' and replace any further '-' in the strings (which separate month and day) by nothing, after which you we convert the resulting strings to numerical values and compare them using >. So for example, three ISO dates like -102-01-01, 70 and 2019-04 would convert to numerics -102.0101, 70 and 2019.04 respectively.

Example

difference = datdif('22/11/62', '07/08/62')

Result

The variable difference is filled with -107

Example for comparing incomplete ISO dates as well

integer i, k
text date_early_text[20], date_late_text[20]
numeric date_early_numeric, date_late_numeric

 

* Execute only when leaving a field in edit mode
if (&1 = 21) {
 if (&4[2] = 'DS') {         /* 'Date (early)'
     i = int(val(&4[3]))
     date_early_text = DS[i]
     date_late_text = DE[i]
     gosub 100
     if ((DS[i] <> '' and DE[i] <> '') and (date_early_numeric > date_late_numeric)) {
        errorm 'End date is before start date!'
        DS[i] = ''
     }
 }
 

 if (&4[2] = 'DE') {         /* 'Date (late)'
     i = int(val(&4[3]))
     date_early_text = DS[i]
     date_late_text = DE[i]
     gosub 100
     if ((DE[i] <> '' and DS[i] <> '') and (date_early_numeric > date_late_numeric)) {
        errorm 'End date is before start date!'
        DE[i] = ''
     }
 }
}

end

 

100 /* convert ISO dates to numerical values to be able to compare incomplete ISO dates as well
  date_early_numeric = 0
  date_late_numeric = 0
  if (date_early_text <> '') {
    if (instr$(1, date_early_text, '-') = 1) {
        k = instr$(2, date_early_text, '-')
        if (len(date_early_text) > 2 and k > 0) {
          k = instr$(k+1, date_early_text, '-')
          if (k > 0) {
              date_early_text = replace$(k, date_early_text, '-', '', 1)
          }
        }
        date_early_text = replace$(2, date_early_text, '-', '.', 1)
        date_early_numeric = val(date_early_text)
      } else {
      k = instr$(1, date_early_text, '-')
      if (len(date_early_text) > 1 and k > 0) {
          k = instr$(k+1, date_early_text, '-')
          if (k > 0) {
              date_early_text = replace$(k, date_early_text, '-', '', 1)
          }
      }
      date_early_text = replace$(1, date_early_text, '-', '.', 1)
      date_early_numeric = val(date_early_text)
    }
  }
 

  if (date_late_text <> '') {
    if (instr$(1, date_late_text, '-') = 1) {
        k = instr$(2, date_late_text, '-')
        if (len(date_late_text) > 2 and k > 0) {
          k = instr$(k+1, date_late_text, '-')
          if (k > 0) {
              date_late_text = replace$(k, date_late_text, '-', '', 1)
          }
        }
        date_late_text = replace$(2, date_late_text, '-', '.', 1)
        date_late_numeric = val(date_late_text)
      } else {
      k = instr$(1, date_late_text, '-')
      if (len(date_late_text) > 1 and k > 0) {
          k = instr$(k+1, date_late_text, '-')
          if (k > 0) {
              date_late_text = replace$(k, date_late_text, '-', '', 1)
          }
      }
      date_late_text =  replace$(1, date_late_text, '-', '.', 1)
      date_late_numeric = val(date_late_text)
    }
  }
return

See also

DATE$