%DATE (Convert to Date)

%DATE{(expression{:date-format})}

%DATE converts the value of the expression from character, numeric, or timestamp data to type date. The converted value remains unchanged, but is returned as a date.

The first parameter is the value to be converted. If you do not specify a value, %DATE returns the current system date.

The second parameter is the date format for character or numeric input. Regardless of the input format, the output is returned in *ISO format.

For information on the input formats that can be used, see Date Data Type. If the date format is not specified for character or numeric input, the default format is *ISO. For more information, see DATFMT(fmt{separator}).

If the first parameter is a timestamp, *DATE, or UDATE, do not specify the second parameter. The system knows the format of the input in these cases.

For more information, see Information Operations or Built-in Functions.

%DATE Examples

  • In the following example, the date value in the string is known to be in *USA format with no separators.
    
    string = '04052024';
    date =  %date(string : *USA0);
    // date = D'2024-04-05'
    
  • In the following example, the value in the numeric variable is known to be in the job date format, but with 4 digits for the year. In this exapmle, the job date format is *DMY. Date format *LONGJOBRUN is used, so the numeric value is assumed to be in the *EUR format, which is the format with 4-digit years related to the *DMY format.
    
    // num = 05042024
    date =  %date(num : *LONGJOBRUN);
    // date = D'2024-04-05'