Convert a Text Answer to a Date

From HotDocs Wiki

Jump to: navigation, search

HotDocs Date variables allow you to prompt for dates during an interview, and then use date formats to control how the answers are formatted when they are merged into the assembled document. Sometimes, however, you may need to use Text variables instead of Date variables for dates in the interview. For example, if you map HotDocs variables to date/time fields in a database, they can be mapped only to Text variables.

When you use a Text variable instead of a Date variable, there is no direct way to change the format in which the date appears in the document. For example, if the text answer is 4/22/1989 10:45 AM, HotDocs cannot automatically format the answer in the assembled document as April 22, 1989. Instead, you must first convert the text answer to a date answer.

Example

The following computation converts a date/time answer stored in a Text variable to a date answer and stores it in a Date variable. This Date variable can then be used in the assembled document and formatted like any other Date variable.

In this computation, Text Var is any Text variable that contains a date/time answer in the format 9/17/2005 8:00 AM.

Date Var is any Date variable. The computation sets this variable's answer to the date found in Text Var.

Month is a temporary Number variable used in the computation.

Day is a temporary Number variable used in the computation.

Year is a temporary Number variable used in the computation.

Caution: This computation assumes that the date/time answer in the Text variable is formatted like 4/22/1989 10:45 AM. (In particular, it requires a forward slash after the month and day, and a space after the year.) If your date/time answer is formatted differently, you must alter the computation to match your answer format:

//Set the computation's RESULT to the answer of the Text variable.
Text Var
 
//Determine the month, then remove the month and forward slash from the computation's RESULT. 
SET Month TO INTEGER( FIRST( RESULT, POSITION( RESULT, "/" ) - 1 ) )
LAST( RESULT, LENGTH( RESULT ) - POSITION( RESULT, "/" ) )
 
//Determine the day, then remove the day and forward slash from the computation's RESULT. 
SET Day TO INTEGER( FIRST( RESULT, POSITION( RESULT, "/" ) - 1 ) )
LAST( RESULT, LENGTH( RESULT ) - POSITION( RESULT, "/" ) )
 
//Determine the year. 
SET Year TO INTEGER( FIRST( RESULT, POSITION( RESULT, " " ) - 1) )
 
//Set the Date variable. 
IF (Day < 1 OR Day > 31 ) OR ( Month < 1 OR Month > 12 ) OR ( Year < 1582 OR Year > 3000 )
  //The date is invalid; do not set the Date variable. 
ELSE
  SET Date Var TO DATE OF( Day, Month, Year ) 
END IF 
//Clear the computation's RESULT.
""