TechTip: RPG Date Tricks

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times
Working with dates can sometimes be complex, with all the APIs, SQL functions, and other methods available. Here are a handful of expressions you can use with date data type fields in pure RPG. For concise code, these expressions can, and probably should, be wrapped in subprocedures:

Deriving the Day of Week

DoWk = %rem(%diff(MyDate:d’0001-01-nn’:*d) : 7) + 1;


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:

ResultDate = MyDate - %days(%subdt(MyDate:*d) - 1) + %days(n - 1);


Make sure MyDate has n days, or else ResultDate will be a date in the following month.

To change the month to n:

ResultDate = MyDate - %months(%subdt(MyDate:*m) - 1) + %months(n - 1);


To change the year to n:

ResultDate = MyDate - %years(%subdt(MyDate:*m) - 1) + %months(n - 1);


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:

MonthDay = (%subdt(MyDate:*m) * 100) + %subdt(MyDate:*d);


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:

NumericISO = (%subdt(MyDate:*y) * 10000) +
             (%subdt(MyDate:*m) * 100)   +
             (%subdt(MyDate:*d);


or

NumericUSA = (%subdt(MyDate:*m) * 1000000) +
             (%subdt(MyDate:*d) * 10000)   +
             (%subdt(MyDate:*y);


Changing the Day of a Date to the First of the Month

ResultDate = MyDate - %days(%subdt(MyDate:*d) – 1);


Changing the Day of a Date to the Last of the Month

ResultDate = MyDate - %days(%subdt(MyDate:*d) – 1) + %months(1) - %days(1);


Changing the Day of a Date to the First of the Year

ResultDate = MyDate - %months(%subdt(MyDate:*m) – 1) –

%days(%subdt(MyDate:*d)- 1);

or

ResultDate = d'0001-01-01' + %years(%subdt(MyDate:*y) - 1)


Determining What Quarter a Date Is In

Quarter = %div(%subdt(MyDate:*m) – 1 : 3) + 1;


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:

((Quarter – 1) * 3) + 1


Determining the Last Date of a Quarter

ResultDate = FirstDate + %months(4) - %days(1);


Deriving a Date from a J.D. Edwards (CYYJJJ) Date

ResultDate = %date(JDEDate + 1900000 : *longjul);


Deriving a J.D. Edwards Date from a Date:

JDEDate = %int(%char(MyDate:*longjul0)) – 1900000;


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:

%diff(MyDate-%days(%subdt(MyDate:*d)) :                   
      d'0001-01-01' + %years(%subdt(MyDate:*y) - 1) : 
      *d)
+ %subdt(MyDate:*d)
+ 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).

Years = %diff(Date1:Date2:*y);
Date1 -= %years(Years);
Months = %diff(Date1:Date2:*m);
Date1 -= %months(Months);
Days = %diff(Date1:Date2:*d);


Converting Character or Numeric Fields to Date

MyDate = %date(MyField:);


Converting Date to Character

MyChar = %char(MyDate:);


Converting Date to Numeric

MyNumeric = %int(%char(MyDate:));


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

TargetDate = %char(%date(SourceDate:):);


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.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: