Tuesday, 1 October 2013

Date field in RPG


Date Field

Date field is always a tricky data type for the programmers. Today I will try to explain the date field to make it easier for all of us. We define a variable in D specification with a data type d.

D  testdate                    s                        d                    // d is the data type

Default value of date variable is 0001-01-01 which also *LOVAL. 9999-12-31 is *HIVAL of a date variable.

There are different types of Date formats. For example

*ISO  :   YYYY-MM-DD    (Default date format)
*YMD:  YY/MM/DD
*USA:    MM/DD/YYYY
*DMY:   DD/MM/YY
*EUR:     DD/MM/YYYY  etc

How to initialize the value in any date field

For initializing the value in date field the value must be preceded by character d and date must be in the single quotes as mention in the below examples.
D  testdate1                    s                           d           INZ(d’2013-10-03’)  
D  testdate2                    s                           d           INZ(d’2013-12-31’)

Now let’s talk about any other date format for understanding more on this.
D testdate1                      s                          d           INZ(d’13/10/03’)

When we compile the above statement in RPG program it will throw a compile time error by saying that “the date literal is not valid” because as I mention in the above that default format of the date is *ISO however in the above example we are dealing with *YMD format. So for this kind of situation we need to mention the date which we want to see in the H specification.

H DATFMT(*YMD)

Now what if we want to subtract or add years/months/days in our date.

Please have a look in the following example for subtraction of one year from the system date.

Dloandate         S               D                                                 
Dduedate          S               D                                            
C                   eval      loandate = %date()                     //  loandate field has the system date   
C     loandate      adddur    1:*years      duedate            //   Adding one year to the system date              
C     duedate       dsply                                                 //   duedate has the one year more the system date
C                   seton                                          lr        

For example my system date is 2013-10-03 so after executing the above RPG Program duedate will be having value 2014-10-03. And observed one thing in the above example that I did not mention the date format so by default it will take the *ISO date format.
So in our case the output will be: 2014-10-03







No comments:

Post a Comment