Date

Converts DateTime, string, or numeric data to data of type date or extracts a date value from a blob. You can use one of several syntaxes, depending on the datatype of the source data.

To

Use

Extract the date from DateTime data or extract a date stored in a blob

Syntax 1

Convert a string to a date

Syntax 2

Combine numeric data into a date

Syntax 3


Platform information for Windows

To make sure you get correct return values for the year, you must verify that yyyy is the Short Date Style for year in the Regional Settings of the user's Control Panel. Your program can check this with the RegistryGet function.

If the setting is not correct, you can ask the user to change it manually or have the application change it (by calling the RegistrySet function). The user may need to reboot after the setting is changed.

Syntax 1: For DateTime data and blobs

Description

Extracts a date from a DateTime value or from a blob whose first value is a date or DateTime value.

Syntax

Date ( datetime )

Argument

Description

datetime

A DateTime value or a blob in which the first value is a date or DateTime value. The rest of the contents of the blob is ignored. Datetime can also be an Any variable containing a DateTime or blob.


Return value

Date.

Returns the date in datetime as a date. If datetime contains an invalid date or an incompatible datatype, Date returns 1900-01-01. If datetime is null, Date returns null.

Examples

After a value for the DateTime variable ldt_StartDateTime has been retrieved from the database, this example sets ld_StartDate equal to the date in ldt_StartDateTime:

DateTime ldt_StartDateTime
date ld_StartDate
ld_StartDate = Date(ldt_StartDateTime)

Assuming the value of a blob variable ib_blob contains a DateTime value beginning at byte 32, the following statement converts it to a date value:

date ld_date
ld_date = Date(BlobMid(ib_blob, 32))

See also

DateTime

Syntax 2: For strings

Description

Converts a string whose value is a valid date to a date value.

Syntax

Date ( string )

Argument

Description

string

A string containing a valid date (such as January 1, 2002, or 12-31-99) that you want returned as a date. Datetime can also be an Any variable containing a string.


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:

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:

// 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:

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

DateTime

IsDate

RelativeDate

RelativeTime

Date method for DataWindows in the section called “Date” in DataWindow Reference

Syntax 3: For combining numbers into a date

Description

Combines numbers representing the year, month, and day into a date value.

Syntax

Date ( year, month, day )

Argument

Description

year

The 4-digit year (1 to 9999) of the date

month

The 1- or 2-digit integer for the month (1 to 12) of the year

day

The 1- or 2-digit integer for the day (1 to 31) of the month


Return value

Date.

Returns the date specified by the integers for year, month, and day as a date datatype. If any value is invalid (out of the range of values for dates), Date returns 1900-01-01. If any argument's value is null, Date returns null.

Examples

These statements use integer values to set ld_my_date to 2005-10-15:

date ld_my_date
ld_my_date = Date(2005, 10, 15)

See also

DateTime

DaysAfter

RelativeDate

RelativeTime