contact image

Date operations allow you to work with dates, times, and timestamp fields

and character or numeric fields that represent dates, times, and 


You can: 

  • Add or subtract a duration in years, months, days, hours, minutes, seconds, or microseconds
  • Determine the duration between two dates, times, or timestamps
  • Extract a portion of a date, time, or timestamp (for example, the day)
  • Test that a value is valid as a date, time, or timestamp.
  • To add or subtract a duration, you can use the + or - operator in free-form
  • syntax or the ADDDUR or SUBDUR operation code in traditional syntax.
  • The code below shows the built-in functions that you use in free-form syntax and the duration codes that you use in traditional syntax.

    Various Date Calculations

    DATE        RPGLE       Show various date processes.

    Download text files

    Built in functions used with date processing

    %MSeconds    Retrieve milliseconds from formatted date
    %Seconds     Retrieve seconds from formatted date
    %Minutes     Retrieve minutes from formatted date
    %Hours       Retrieve hours from formatted time
    %Days        Retrieve days from formatted date
    %Months      Retrieve months from formatted date
    %Years       Retrieve years from formatted date
    %Date        Retrieve the date        
    %Time        Retrieve the time
    %TimeStamp   Retrieve a time stamp
    %Subdt       Substring year, month or day from formatted date
    %Char        Use a decimal field in a substring function
        Define Date Variables All date formats without keyword
    	DatFmt will default to *ISO.  '2003-09-23'
    	When you initialize a date field you must prefix the
    	date with the letter "D" see below.
         D*  Field Definitions.
         D                SDS
         D  @PGM                 001    010
         D  @PARMS               037    039  0
         D  @JOB                 244    253
         D  @USER                254    263
         D  @JOB#                264    269  0
         D*  Field Definitions.
         D ISOdate         S               D
         D USAdate         S               D   DatFmt(*USA)
         D XMASDate        S               D   Inz(D'2003-12-25')
         D LogonDate       S               D
         D Date_Start      S             15
         D MonthNames      S             12    Dim(12) CtData
         D Date_String     S             40
          * Time Stamp
         DTimeStamp        S               Z
         D WorkISO         S               D
         D Month           S              2  0
         D Day             S              2  0
         D Year            S              4  0
         D Decimal8        S              8  0
         D LogMonth        S              2  0
         D LogDay          S              2
         D LogYear         S              4  0
         D NextMonth       S               D
         D EndOfMonth      S               D
          * Variables for free RPG example + some above
         D DateIn          S               D
         D FromISO         S               D
         D ToISO           S               D
         D DiffDays        S              3  0
         D WorkField       S              5  0
         D Name            S              9    Based(NamePtr)
         D Name2           S              9
         D NamePtr         S               *   Inz(%ADDR(Names))
         D Names           S             63    Inz('Sunday   Monday   Tuesday  Wedn+
         D                                     esdayThursday Friday   Saturday ')
          * RPG-defined date formats and separators for Date data type
          * 2-Digit Year Formats
          * *MDY  Month/Day/Year  mm/dd/yy  8  09/26/03
          * *DMY  Day/Month/Year  dd/mm/yy  8  26/09/03
          * *YMD  Year/Month/Day  yy/mm/dd  8  03/09/26
          * *JUL  Julian          yy/ddd    6  03/926
          * 4-Digit Year Formats
          * *ISO  Int Standards Org yyyy-mm-dd  10  2003-09-26
          * *USA  IBM USA Standard  mm/dd/yyyy  10  09/26/2003
          * *EUR  IBM European Std  10  26.09.2003
          * *JIS  Japan Indst Std   yyyy-mm-dd  10  2003-09-26
          * Okay first lets get todays date
          * For display purposes the date is now
          *      Friday September 26th 2003
          *  so date now looks like this 2003-09-26
          *  because the default date type is *ISO
         C                   Eval      ISOdate = %Date()
          *  Now that I have the date in a date format
          *  (*ISO) I can do stuff to it.
          *  Once I move this date to a decimal 8,0 field
          *  the date is now in format 20030926
          *  Not very exciting..yet
         C                   Move      ISODate       Decimal8
          *  Now back to the *ISO date  lets add
          *  1 month to the date.
          *  date after will equal 2003-10-26
          *  %days and %years works the same as %months
         C                   Eval      WorkISO  = ISODate + %Months(1)
          *  Logon date is set equal to today then the month is extracted
          *  the "*M" is the same as "*Months"  LogMonth = 09.
          *                                     LogDay   = 26.
         C                   Eval      LogonDate =  %Date()
         C                   Extrct    LogonDate:*Y  LogYear
         C                   Extrct    LogonDate:*M  LogMonth
         C                   Extrct    LogonDate:*D  LogDay
          *  Build the date string - Later we will add the day name
         D MonthNames      S             12    Dim(12) CtData
         C                   Eval      Date_String =
         C                              %Trim(MonthNames(LogMonth))
         C                              + %trim('@') + %Trim(LogDay)
         C                              + %trim(',@') + %Char(LogYear)
    ** CTDATA MonthNames