$Sir DateDif: Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (add link)
m (add link)
Line 22: Line 22:


<tr><th>fmta</th>
<tr><th>fmta</th>
<td>First datetime format string. Refer to [[Datetime string formats]] for an explanation of valid datetime formats and valid datetime values. Strict matching is used for <var class="term">fmta</var>.</td></tr>
<td>First datetime format string. Refer to [[Datetime string formats]] for an explanation of valid datetime formats and valid datetime values. [[Datetime string formats#Strict and non-strict format matching|Strict matching]] is used for <var class="term">fmta</var>.</td></tr>


<tr><th>data</th>
<tr><th>data</th>
Line 28: Line 28:


<tr><th>fmtb</th>
<tr><th>fmtb</th>
<td>Optional second datetime format string; the default is to use <var class="term">fmta</var>. [[Datetime string formats#Strict and non-strict format matching|Strict matching]] is used for <var class="term">fmtb</var>.</td></tr>
<td>Optional second datetime format string; the default is to use <var class="term">fmta</var>. Strict matching is used for <var class="term">fmtb</var>.</td></tr>


<tr><th>datb</th>
<tr><th>datb</th>

Revision as of 23:43, 24 September 2015

Difference between two dates

Note: This $function require the Sir2000 User Language Tools product.

The $Sir_DateDif function expects a datetime format string and two datetime value strings. It accepts a second optional datetime format string, an optional CENTSPAN value, and an optional error control string.

$Sir_DateDif subtracts the second datetime value from the first datetime value, returning the number of complete days of difference. If an error is detected, the value 99999999 is returned.

Syntax

%dif = $Sir_DateDif(fmta, data, [fmtb], datb, [span], [errctl])

Where:

%dif Set to the number of days obtained from subtracting datb from data. If an error is detected, the value returned is 99999999.
fmta First datetime format string. Refer to Datetime string formats for an explanation of valid datetime formats and valid datetime values. Strict matching is used for fmta.
data First datetime value string.
fmtb Optional second datetime format string; the default is to use fmta. Strict matching is used for fmtb.
datb Second datetime value string.
span Optional CENTSPAN value; the default is -50. Refer to Using CENTSPAN.
errctl Optional error control string. Refer to Datetime error handling.

Example

The following fragment prints the string -7 days:

Print $sir_datedif('YYMMDD', '970301', , '970308') and 'days'

Error conditions

$Sir_DateDif returns the value 99999999 in the following error cases:

  • fmta or fmtb is not a valid datetime format.
  • data or datb does not match the respective datetime format.
  • data or datb is outside of range permitted for the respective datetime format.
  • span is invalid.

For more information about error handling, see the APPDATE command and see Sir_Date_Err.

Usage notes

  • Time is ignored in the subtraction.
  • In the example above, even though the input dates have 2-digit years, the correct answer would be given for any valid span argument, since the dates do not span the end of February.
  • As in the standard SOUL $DateDif function, the single span argument is used for both dates. If it is necessary to get the difference between two dates that both have 2-digit years and are in different 100-year windows, you must first use $Sir_DateCnv to convert one of them to some 4-digit year format. Alternatively, you could use $Sir_Date2ND to convert both dates to number of days values, and subtract those values.