Adding or subtracting time from a date-time value

Problem

You need to add or subtract time (seconds, minutes, hours, days) from a date-time value expressed as YYYYMMDD or YYYYMMDDHHMMSS.

Solution

Use the add_to_date function in a CALCUL question.

Discussion

CallWeb stores dates as YYYYMMDD values and date-time as YYYYMMDDHHMMSS values. For example, $contexte{date} contains the current date formatted as YYYYMMDD (currently 20240420) and $contexte{dateheure} contains the current date and time formatted as YYYYMMDDHHMMSS (currently 20240420043959). While comparisons are easy, other manipulations of such values are not. Adding one day to 20091231 returns 20100101, for example. The add_to_date function helps with date and date-time calculations.

Here is the general syntax of the add_to_date function:

ADATE2 = add_to_date($ADATE1,value,type_of_offset)

where

  • ADATE2 is the output variable of the CALCUL question
  • $ADATE1 is the initial date or date-time value; a date comprises 8 characters (YYYYMMDD) while a date-time includes 14 characters (YYYYMMDDHHMMSS)
  • value is the amount that needs to be added or subtracted; to add one day, value would be 1; this value can be a constant (like "1") or a variable (like "$Q1");
  • type_of_offset expresses the type of amount to add or subtract; it is a keyword among the following: DAY, DAYS, JOUR, JOURS, HOUR, HOURS, HEURE, HEURES, MINUTE, MINUTES, SECOND, SECONDS, SECONDE, SECONDES

So, add_to_date(20091231,1,"DAY") adds one day to New Year's Eve 2009 while add_to_date(20091231235959,1,"SECOND") moves from 2009 to 2010. Nevative values subtract from the stated date.

The result returned by add_to_date has the same length as the input value. Thus, adding less than one day to a YYYYMMDD value will return the same date value.

Adding or subtracting time from a date-time value

Problem

You need to add or subtract time (seconds, minutes, hours, days) from a date-time value expressed as YYYYMMDD or YYYYMMDDHHMMSS.

Solution

Use the add_to_date function in a CALCUL question.

Discussion

CallWeb stores dates as YYYYMMDD values and date-time as YYYYMMDDHHMMSS values. For example, $contexte{date} contains the current date formatted as YYYYMMDD (currently 20240420) and $contexte{dateheure} contains the current date and time formatted as YYYYMMDDHHMMSS (currently 20240420043959). While comparisons are easy, other manipulations of such values are not. Adding one day to 20091231 returns 20100101, for example. The add_to_date function helps with date and date-time calculations.

Here is the general syntax of the add_to_date function:

ADATE2 = add_to_date($ADATE1,value,type_of_offset)

where

  • ADATE2 is the output variable of the CALCUL question
  • $ADATE1 is the initial date or date-time value; a date comprises 8 characters (YYYYMMDD) while a date-time includes 14 characters (YYYYMMDDHHMMSS)
  • value is the amount that needs to be added or subtracted; to add one day, value would be 1; this value can be a constant (like "1") or a variable (like "$Q1");
  • type_of_offset expresses the type of amount to add or subtract; it is a keyword among the following: DAY, DAYS, JOUR, JOURS, HOUR, HOURS, HEURE, HEURES, MINUTE, MINUTES, SECOND, SECONDS, SECONDE, SECONDES

So, add_to_date(20091231,1,"DAY") adds one day to New Year's Eve 2009 while add_to_date(20091231235959,1,"SECOND") moves from 2009 to 2010. Nevative values subtract from the stated date.

The result returned by add_to_date has the same length as the input value. Thus, adding less than one day to a YYYYMMDD value will return the same date value.