How to validate a 6-character field in RPGLE to ensure it contains a valid date in MMDDYY format

Handling dates has always been a tricky task for RPG programmers, often requiring the modernization of old date routines written in RPG3, RPG400, or even modern RPGLE that still use legacy styles. In this lesson, we’ll explore the TEST operation code and the %DATE monitoring technique as clear, clean alternatives in RPGLE.

To validate a 6-character field in RPGLE to ensure it contains a valid date in MMDDYY format, you have several options:

Option 1: Using TEST(DE) Operation Code (Recommended)

dcl-s dateField char(6);
dcl-s isValid ind;

// Test if valid date in MMDDYY format
test(de) *mdy dateField;
isValid = not %error();

if isValid;
// Date is valid
else;
// Date is invalid
endif;

Option 2: Using %DATE() BIF with Monitor

dcl-s dateField char(6);
dcl-s validDate date;
dcl-s isValid ind inz(*off);

monitor;
validDate = %date(dateField : *mdy);
isValid = *on;
on-error;
isValid = *off;
endmon;

if isValid;
// Date is valid
else;
// Date is invalid
endif;

Option 3: Manual Validation (More Control)

dcl-s dateField char(6);
dcl-s month packed(2:0);
dcl-s day packed(2:0);
dcl-s year packed(2:0);
dcl-s isValid ind inz(*off);

// Extract components
month = %dec(%subst(dateField:1:2):2:0);
day = %dec(%subst(dateField:3:2):2:0);
year = %dec(%subst(dateField:5:2):2:0);

// Basic validation
if month >= 1 and month <= 12
and day >= 1 and day <= 31;

// Use TEST for final validation
test(de) *mdy dateField;
isValid = not %error();
endif;

Option 4: In Display File (DDS Level)

You can also add validation directly in the display file:

A DATFLD 6M B 10020CHECK(RB)
A DATFMT(*MDY)
A DATSEP('/')

Best Practice: Use Option 1 (TEST(DE)) as it's the most efficient and cleanest approach. The TEST operation code with *MDY format will validate:

  • Month is 01-12
  • Day is valid for the month (including leap year checking)
  • All characters are numeric

The %ERROR() BIF returns *ON if the test fails, so not %error() gives you a boolean indicating validity.

In Summary

To validate a 6-character field in RPGLE for a valid date in MMDDYY format, use the `TEST(DE)` operation code:

My Preferred Approach:
dcl-s dateField char(6);
dcl-s isValid ind;

// Test if valid date in MMDDYY format
test(de) *mdy dateField;
isValid = not %error();

if isValid;
// Date is valid
else;
// Date is invalid - show error message
endif;

But sometimes I like to use the Built In Function within a monitoring block. Both techniques work well - so pick your favourite!

Alternative with %DATE() BIF:

dcl-s dateField char(6);
dcl-s validDate date;
dcl-s isValid ind inz(*off);

monitor;
validDate = %date(dateField : *mdy);
isValid = *on;
on-error;
isValid = *off;
endmon;

Of course, if using a DSPF to enter dates you can ensure the user can ONLY enter valid dates like this:

A      DATFLD             6M B 10020CHECK(RB)
A                                   DATFMT(*MDY)

The TEST(DE) operation validates that the month is 01-12, day is valid for the month (including leap year checking), and all characters are numeric.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>