Syntax 2: For strings
Description
Converts a string whose value is a valid date to a date
value.
Syntax
1 |
Date ( string ) |
Argument |
Description |
---|---|
string |
A string containing a valid date (such as January |
Return value
Date.
Returns the date in string as a date. If string contains an
invalid date or an incompatible datatype, Date returns 1900-01-01. If
string is null, Date returns null.
Usage
Valid dates in strings can include any combination of day (1 to
31), month (1 to 12 or the name or abbreviation of a month), and year (2
or 4 digits). PowerBuilder assumes a 4-digit number is a year. Leading
zeros are optional for month and day. The month, whether a name, an
abbreviation, or a number, must be in the month location specified in
the system setting for a date’s format. If you do not know the system
setting, use the standard datatype date format yyyy-mm-dd.
PowerBuilder attempts to match the input string to a date format
in the regional settings on the computer. If a complete match is not
found, PowerBuilder attempts a partial match. For example, if you use
Date(’01-JAN-1900′) and PowerBuilder finds the partial match
(dd-MMM-yy), PowerBuilder parses the first two numbers of the year and
gets 19. The 2-digit year is interpreted as a year between 1930 and
2029, and the date returned is 1/1/2019.
Date literals do not need to be converted with the Date
function.
Examples
Example 1
These statements all return the date datatype for text expressing
the date July 4, 2004 (2004-07-04). The system setting for a date’s
format is set with the month’s position in the middle:
1 2 3 |
Date("2004/07/04") Date("2004 July 4") Date("04 July 2004") |
Example 2
The following groups of statements check to be sure the date in
sle_start_date is a valid date and display a message if it is not. The
first version checks the result of the Date function to see if the date
was valid. The second uses the IsDate function to check the text before
using Date to convert it:
Version 1:
1 2 3 4 5 6 7 8 |
// Windows Control Panel date format is YY/MM/DD date ld_my_date ld_my_date = Date(sle_start_date.Text) IF ld_my_date = Date("1900-01-01") THEN MessageBox("Error", "This date is invalid: " & + sle_start_date.Text) END IF |
Version 2:
1 2 3 4 5 6 7 8 |
date ld_my_date IF IsDate(sle_start_date.Text) THEN ld_my_date = Date(sle_start_date.Text) ELSE MessageBox("Error", "This date is invalid: " & + sle_start_date.Text) END IF |
See also
Date method for DataWindows in the section called “Date” in DataWindow Reference