top of page

46. Convert a Text String to a Date Value

Description

Converts a string containing a date to a HotDocs date value.

Code

Explanation
By now you know that HotDocs has four data types: text, number, date, and true/false. Just because a text string contains a date does not make it a date value. It must be converted to a HotDocs date value before date formatting or calculations can be performed on it. This is an issue if you are trying to read dates from .ini files or from databases that are passing the dates as text.

The computations below will convert a text string formatted as "06/03/1993" or "June 3, 1993" to a HotDocs date value. They can be modified to fit nearly any format.

The the model template contains a converter which is much more flexible and is very forgiving of format for situations where the format will be unpredictable. It correctly recognized all of the following as "July 24, 1976":

7/24/1976
24 jul 1976
7-24-1976
July 24, 1976
jul 24 76
The model template's converter is ready to use without any modification. It could not be included here because of complexity.

Note: All of these computations attempt to take the month before the day. The model template also contains a second converter that will favor the day over the month for non-US locales.


Code
Date string is formatted as 06/03/1993:

DATE OF(
(INTEGER( FIRST( TextVar, 2 ))),
(INTEGER( MID( TextVar, 4, 2 ))),
(INTEGER( LAST( TextVar, 4 )))
)


Date string is formatted as June 3, 1993:

// MONTH
SET Temp-n TO POSITION( TextVar, " " )
SET Temp-t TO FIRST( TextVar, Temp-n - 1 )
SET Temp-t TO FORMAT( Temp-t, "like this" )

IF Temp-t CONTAINS "jan"
SET Month-n TO 1
ELSE IF Temp-t CONTAINS "feb"
SET Month-n TO 2
ELSE IF Temp-t CONTAINS "mar"
SET Month-n TO 3
ELSE IF Temp-t CONTAINS "apr"
SET Month-n TO 4
ELSE IF Temp-t CONTAINS "may"
SET Month-n TO 5
ELSE IF Temp-t CONTAINS "jun"
SET Month-n TO 6
ELSE IF Temp-t CONTAINS "jul"
SET Month-n TO 7
ELSE IF Temp-t CONTAINS "aug"
SET Month-n TO 8
ELSE IF Temp-t CONTAINS "sep"
SET Month-n TO 9
ELSE IF Temp-t CONTAINS "oct"
SET Month-n TO 10
ELSE IF Temp-t CONTAINS "nov"
SET Month-n TO 11
ELSE
SET Month-n TO 12
END IF

// DAY
SET Temp-t TO LAST( TextVar, LENGTH( TextVar ) - Temp-n )
SET Temp-n TO POSITION( Temp-t, "," )
SET Day-n TO INTEGER( FIRST( Temp-t, Temp-n - 1 ))

// YEAR
SET Year-n TO INTEGER( LAST( TextVar, 4 ))

// PUT IT TOGETHER (Must be a valid date!!!)
DATE OF( Day-n, Month-n, Year-n )


Explanation
To convert a date string (text value) to a date value, you must separate the string into its day, month and year sections, determine a numeric value for each section, then pass these values to the DATE OF() model for conversion.

If the date string is in the format "06/09/1990", parsing the string is as simple as taking the first two characters for the month, characters 4 & 5 for the day, and the last four characters for the year. After converting each of these to an integer, a date value can be returned.

If the date string is in the format "June 9, 1990", parsing and converting the string is more detailed. Since the length of the month and day in characters can vary, we must parse the string based on the first space and the comma. In other words, we take everything before the first space as the month, everything between the first space and the comma as the day, and the last four characters as the month.

This conversion requires some temporary variables (in the Advanced options for each, select "ask only in dialog", "don't warn if unanswered", and "don't save in answer file"): Temp-n, a number variable used to store the position of the space and the comma in the string, Temp-t, a text variable used to store segments of text extracted from the string, Day-n, a number variable used to store the numeric value of the day, Month-n, a number variable used to store the numeric value of the month, and Year-n, a number variable used to store the numeric value of the year.

Explanation

bottom of page