Deriving the Day of Week
To return a number based on 1=Monday, use 01 for nn. For 1=Sunday, use 07 for nn.
Changing a Component of a Date
RPG does not have a facility to easily change a component of a date, so if you do not want to use an overlapping data structure with a pointer, you can use expressions such as the following:
To change the day to n:
Make sure MyDate has n days, or else ResultDate will be a date in the following month.
To change the month to n:
To change the year to n:
In changing the day or month, it would generally be safe to omit the "- 1" factor. It will be needed, however, when changing the components of a date initially set to *LOVAL.
Extracting Multiple Components of a Date into One Field
This requires a little mathematics. As an example, to get the month and day into one numeric field, do this:
Think of the zeros as place holders for all of the following components. This method of extracting multiple components is required to convert a date to numeric in pure RPG prior to V5R2, like so:
(%subdt(MyDate:*m) * 100) +
(%subdt(MyDate:*d) * 10000) +
Changing the Day of a Date to the First of the Month
Changing the Day of a Date to the Last of the Month
Changing the Day of a Date to the First of the Year
Determining What Quarter a Date Is In
Determining the First Date of a Quarter
The year has to be known. The day will be 1. The month is determined with this code:
Determining the Last Date of a Quarter
Deriving a Date from a J.D. Edwards (CYYJJJ) Date
Deriving a J.D. Edwards Date from a Date:
Deriving the Julian Day
Issuing a %subdt or extrct on a Julian formatted date will return the Gregorian day. To get the Julian day, try this expression:
d'0001-01-01' + %years(%subdt(MyDate:*y) - 1) :
This expression should operate on MyDate, regardless of its format, Julian or otherwise.
Deriving a Calendar Duration
A calendar duration is defined as the duration between two dates in years, months, and days. Date1 should be after Date2 (chronologically speaking).
Date1 -= %years(Years);
Months = %diff(Date1:Date2:*m);
Date1 -= %months(Months);
Days = %diff(Date1:Date2:*d);
Converting Character or Numeric Fields to Date
Converting Date to Character
Converting Date to Numeric
The desired return format should not have separators, so place a 0 following the format, e.g., *iso0, *usa0, etc. You can also use %uns or %dec instead of %int.
Converting a Non-Date Data Type Field Containing a Date from One Format to Another
SourceDate can be character or numeric. This expression would return a character date. To return a numeric date, enclose the expression in an %int, %uns, or %dec. Be sure that the desired format does not contain separators.
You may have other pure RPG date tricks like these. Feel free to share them with other readers in the forums discussion associated with this TechTip.
Doug Eckersley is the iSeries programmer for a premier homebuilder in Columbus. He has been programming on the iSeries for 10 years, has been in the business for 15 years, and is certified by IBM. He is also a patient Blue Jackets fan.