How About a Date (Field)?

This month we highlight another under-used RPG IV capability. The focus this time around is on date fields.

Support for "real" dates (i.e., the D data type) was introduced with the first release of RPG IV and has improved with every release. V5R1, in particular, added some new features, but we're getting ahead of ourselves here.

Let's first review the basics. We'll do so by featuring the EndOfMonth subprocedure that even veteran date-users should find useful. (Note: For simplicity, we've included the subprocedure in the same source as its caller; for reuse purposes of course, it should be separately compiled and included in a service program.)

The D spec marked defines the field Input that well use to hold the input "date." It is, in fact, a six-character field. Some may use "dates" in this format in display and database files. Because RPG IVs date operations only work with real dates, we must move this pretend date into a real date field called "InputDate." InputDate is defined at point . It has a data type of D but no length is defined; we could define the length, but why type more than necessary? We have, however, used the DATFMT keyword to specify that the field be in *USA format. Without that definition, the field would be in the *ISO format, which is the default if no DATFMT keyword is included on the H spec.

At you can see the MOVE operation that converts the Input character field into date format and loads it into InputDate. Before doing this, ensure that Input contains a valid date. That's the purpose of the TEST operation at :

  • The entry *MDY0 in Factor 1 informs the compiler that the source field should contain a date in MDY format. The "0" (zero) indicates that although Input is a character field, it doesn't contain any separator characters.
  • The operation extenders D and E indicate that a date test is to be performed and that the built-in %ERROR is to be set in case an invalid date is detected.

Notice that the *MDY0 entry is also required for the MOVE operation; otherwise the compiler assumes that the source and target fields have the same format.


Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.

Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.

comments powered by Disqus



2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

New and Improved XML-INTO

Namespace support makes the opcode a viable option

Authenticating on the Web

The finer points of OpenRPGUI, Part 1

The Microphone is Open

Add your voice: Should IBM i include open-source RPG tools?

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters