I want to use the date instead of the timestamp value. How can I convert the timestamp type value to the date value in the RPG program?
How can I extract the date portion from a timestamp value? Provide a simple RPGLE program example where we declare and initialize a timestamp variable and convert it to a date format.
You can use the
%DATE
built-in function (bif) that converts timestamp to date.The syntax of the
%DATE
bif is as follows:%DATE(expression:date-format)
Here,
%DATE
bif has 2 optional parameters namely expression which can be character, numeric, or timestamp data, and date-format for character and numeric input in expression.If the first parameter expression is not specified then it returns the current system date and if the second parameter date format is not specified for character and numeric expression then the default format of the date returned is *ISO.
If the first parameter is a timestamp,
*DATE
, orUDATE
then please do not specify the second parameter. The system already knows the input format in these cases.Here is the source code that makes use of
%date
bif to convert timestamp to date in the rpgle program.Line 1: Declaring a timestamp variable
timestamp1
.Line 2: Declaring a date variable
date1
.Line 3 & 11:
/free
and/end-free
block for rpg free code.Line 4: Converting the timestamp value in character to timestamp data using
%timestamp()
bif and evaluating intimestamp1
variable of type timestamp.Line 5: Passing the timestamp value held in variable
timestamp1
as a parameter to the %date bif to convert the timestamp to the date and evaluating in date1 variable of type date. Therefore, the return date to date1 variable would be2024-03-16
.Line 6: Passing the timestamp value in character format to the
%timestamp
bif to first convert it to timestamp data type value and then passing that to %date bif to convert timestamp type value to date type value and evaluating in date1 variable of type date. Therefore, the return date to date1 variable would be2024-03-16
.Line 7: Passing the Current timestamp value using
%timestamp()
bif to%date
bif to convert the current timestamp to date and evaluate in thedate1
variable.Date1
variable would hold2024-09-02
which is the current date.Line 8: Evaluating the current timestamp value using
%timestamp()
bif totimestamp1
variable of type timestamp.Line 9: Passing the timestamp value held in variable
timestamp1
as a parameter to the%date
bif to convert the timestamp to the date and evaluating indate1
variable of type date. Therefore, the return date to date1 variable would be2024-09-02
which is the current date.Line 10: return from the program.