Blog Archive

RPGLE initialize date from literal string

d myDate          s               d   inz(d'2011-05-17')
There are two things of note here. First, to set the value of a date field with a literal, it must be preceded by the letter d and wrapped in single quotation marks ('). This is also true for comparing date values in conditional statements:
if myDate = d'2011-05-17' ;
 // code
endif ;
The format used to initialize the date is *ISO: the default DATFMT (date format) for date literals is *ISO. To initialize dates in a different format you must specify the compiler instruction in H-spec.
 h DATFMT(*USA)
Now all the date fields in your program require literals to be in the *USA format. Whatever format you use, you must be consistent throughout your program. Date fields can be assigned values from other date fields, so if you have a date in *ISO format and you want to display it in *USA, simply move the value into a field defined as *USA and display that field:
d myDateISO        s               d   datfmt(*ISO) inz(d'2011-05-17')
d myDateUSA        s               d   datfmt(*USA) inz(d'2010-03-23')
d myDateString     s             10a

 /free
    myDateUSA = myDateISO ;
    dsply myDateUSA ;
    *inlr = *on ;
 /end-free
Source: ITjungle by Joel Cochran, Read More
Learn about RPGLE date formats