ParseDateFormat Function

Action

Parses a string containing a date and validates each component of the date by comparing it to a specified format. The string must contain a day, month, and year to be considered a valid date.

Syntax

bValidDate = ParseDateFormat (sDate, sFormat [, dtParsedDate, iThreshold, iMinYear, iMaxYear, iAging ] )
Variable Description
bValidDate TRUE if sDate is a valid date of the format sFormat and passes the iMinYear and iMaxYear tests; otherwise, FALSE. BOOLEAN.
sDate The string containing the date to parse. STRING.
sFormat The date format to compare sDate against. See below for how to specify. STRING.
dtParsedDate Optional: Output variable to hold the parsed date if bValidDate is TRUE. Not set if bValidDate is FALSE. DATETIME (the time component is undefined).
iThreshold Optional: Specifies how to convert a two-digit year in sDate to a four-digit year (19xx or 20xx). If the year in sDate is less than or equal to iThreshold, the year in dtParsedDate is 20xx. If the year in sDate is greater than iThreshold, the year in dtParsedDate is 19xx. INTEGER.
iMinYear Optional: A factor in considering whether the date passes validation: If the date after transformation (after applying iThreshold and iAging) is less than iMinYear, bValidDate is set to FALSE. Specify a four-digit year. INTEGER.
iMaxYear Optional: A factor in considering whether the date passes validation: If the date after transformation (after applying iThreshold and iAging) is greater than iMaxYear, bValidDate is set to FALSE. Specify a four-digit year. INTEGER.
iAging Optional: Number of years to add to the date after transformation (after application of iThreshold). INTEGER.

Leap years: The date February 29 will be aged to February 28 if the resulting year is not a leap year.

Notes

  • ParseDateFormat is used automatically in the Year 2000 date transformations specified in Runtime Options, but you can also call the function manually.

  • ParseDateFormat takes a string sDate and determines whether it is a valid date that conforms to a specified format sFormat. The function returns TRUE if sDate is a valid date and, optionally, stores the date as the DATETIME variable dtParsedDate. You can specify how you want two-digit years (such as 97) converted to four-digit years using the optional iThreshold argument. You can specify a range of years within which the converted date must fall in order for the function to return TRUE using iMinYear and iMaxYear.

  • Even though you may specify the format of the date string, the contents of the string are interpreted based on the 4Test locale.

Specifying sFormat

The date format consists of date masks and optional delimiters.

You can use the following masks to specify components of the date in sFormat.

Date mask Description
yy Two-digit year.
yyyy Four-digit year.
m Month with one or two digits (1–12).
mm Month with exactly two digits (01–12).
mmm Abbreviated month name (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, or Dec); case insensitive.
mmmm Full month name (January, February, and so on); case insensitive.
d Day of month with one or two digits (1–31).
dd Day of month with exactly two digits (01–31).
ddd Abbreviated day of the week (Sun, Mon, Tue, Wed, Thu, Fri, or Sat); case insensitive. Allowed in sFormat but ignored by ParseDateFormat.
dddd Full day of the week (Monday, Tuesday, and so on); case insensitive. Allowed in sFormat but ignored by ParseDateFormat.

Delimiters for sFormat

Valid delimiter Description
(Space) One or more spaces
/ Forward slash
- Hyphen
: Colon
, Comma
. Period

Sample Date Formats for sFormat

Format Matches these date strings
m/d/yy 9/18/97, 09/18/97, 9/18/30
mm/dd/yy 09/18/97 (but not 9/18/97)
m-d-yy 9-18-97, 09-18-97
yy/m/d 97/9/18, 97/09/18
mmddyy 091897
mm/dd/yyyy 9/18/1997, 09/18/1997, 9/18/1930, 9/18/2030
yyyy/m/d 1997/9/18, 1997/09/18
mmm d, yyyy Sep 18, 1997

Example

This example passes three strings to ParseDateFormat:

[-] testcase ParseDateFormatTest () appstate none
	[ ] STRING sDate1, sDate2, sDate3, sFormat
	[ ] INTEGER iThreshold
	[ ] DATETIME dt = "1900-12-25" // set to dummy value in case parsing
	[ ] // fails and variable is not set by function
	[ ] sDate1 = "05/30/51"
	[ ] sDate2 = "09/21/04"
	[ ] sDate3 = "09-21-04"
	[ ] sFormat = "mm/dd/yy" // date format to compare strings to
	[ ] iThreshold = 30 
	[ ] 
	[ ] Print (ParseDateFormat(sDate1,sFormat,dt,iThreshold))
	[ ] Print (dt) 
	[ ] Print (ParseDateFormat (sDate2,sFormat,dt,iThreshold))
	[ ] Print (dt)
	[ ] Print (ParseDateFormat (sDate3,sFormat,dt,iThreshold))
	[ ] Print (dt)
	[ ] 
	[ ] // Result:
	[ ] // TRUE // sDate1 parsed as valid date of format sFormat
	[ ] // 1951-05-30 // greater than threshold, so year is 1951
	[ ] // TRUE // sDate2 parsed as valid date of format sFormat
	[ ] // 2006-09-21 // less than threshold, so year is 2006
	[ ] // FALSE // sDate3 did not parse as a valid date of format sFormat
	[ ] // 2006-09-21 // dt unchanged (not set by third call to function)