Back to: ILE RPG in Easy Bytes

Unit 7 covers the native date-related data types. It examines how to define dates and how their format affects processing. In this unit, you discover how to use several built-in functions to easily perform date arithmetic. You also learn how to convert among date, character, and numeric data types, and how to process legacy dates that are defined as numeric or character data.
This course is a unit of the ILE RPG in Easy Bytes series, a complete introductory ILE RPG programming self-guided tutorial.
- Before starting this course you must complete the required prerequisite course: 6. Character Data – ILE RPG in Easy Bytes
One-Time Payment
Members Only
Instead of paying for each unit individually, you may purchase a membership which will automatically enroll you at a discount in the entire “ILE RPG in Easy Bytes” series.
Working with Dates
Date Time Timestamp Date and time are self-evident data types you use to store values associated with the calendar and the clock. A timestamp is a combination of a date and a time. Generally, ILE RPG applies the same principles and rules to all three data types.
The default external format for dates is a 10-byte field with format YYYY-MM-DD. Times have a default length of eight bytes with format hh.mm.
As you might guess, ILE RPG also uses Eval with dates. To assign a value to a date, time, or timestamp variable, you code a simple assignment expression: Dcl-s Enrolldate Date; Dcl-s Today Date Inz(*Sys); Enrolldate = Today; // Enrolldate now contains the current system date This example assigns the value in one date variable to another date variable.
Typically, a date expression includes the arithmetic operators + (addition) and – (subtraction), as well as one or more functions that ILE RPG uses to convert a numeric value to a special internal data type called a duration. Duration is the term RPG uses to describe a unit of time between two dates, times, or timestamps. ILE RPG recognizes seven durations and provides seven corresponding functions:
%Diff, to find the difference between two dates, times, or timestamps %Subdt, to extract a portion of a date, time, or timestamp %Diff (Difference) The %Diff (Difference) function calculates the duration between two date or time values. The first two arguments must be data items of compatible types, then the third argument represents the duration code that corresponds to the duration unit you want to determine. The general format is as follows:
The duration functions examined earlier convert a numeric expression to a duration subtype so it can be used in a date expression. Several other functions convert numeric and character expressions to dates, times, and timestamps, and others convert date-related data back to character and numeric values. These functions are as follows:
Adddur (Add Duration) Subdur (Subtract Duration) Extrct (Extract Date/Time/Timestamp) These operation codes are not as flexible as the date-related functions and are restricted to the fixed-format Calculation specifications. In addition to these operations, you can use the Move and Movel operations in legacy programs to assign and convert date data types. Adddur (Add Duration) The Adddur (Add Duration) operation lets you add a duration coded in Factor 2 to the date specified in Factor 1, storing the answer in the date, time, or timestamp field indicated as the Result field.
- Before starting this course you must complete the required prerequisite course: 6. Character Data – ILE RPG in Easy Bytes