Skip to main content

Extract a portion of a Date/Time/Timestamp in RPGLE - IBM i


Extracting Year, Month, Day, Hour, Minutes, Seconds or Milli seconds of a given Date/Time/Timestamp is required most of the times. 

This can be extracted easily by using %SUBDT. BIF name looks more similar to %SUBST which is used to extract a portion of string by passing from and two positions of the original string. Instead, We would need to pass a value (i.e., Date, Time or Timestamp ) and Unit (i.e., *YEARS, *MONTHS, *DAYS, *HOURS, *MINUTES, *SECONDS or *MSECONDS) to %SUBDT. 

Valid unit should be passed for the type of the value passed. Below are the valid values for each type.
  • Date - *DAYS, *MONTHS, *YEARS


%SUBDT(value : unit { : digits { : decpos} })

  • Value and Unit are the mandatory arguments. 
  • Digits and Decimal positions are optional and can only be used with *SECONDS for Timestamp.
We can either pass the full form for the unit or use the short form. Below is the mapping of Full form to short form. 
  • *YEARS - *Y
  • *MONTHS - *M
  • *DAYS - *D
  • *HOURS - *H
  • *MINUTES - *MN
  • *SECONDS - *S
Let's have a look at couple of simple examples to see how this works. 


Extract Date, Month and Year from a Date field. 

Extract Date, Month and Year from Date using %SUBDT - RPGLE

In the above example, 
  • We are using two different date formats *ISO & *DMY which shows different number of digits for year (*ISO - 4 digits and *DMY - 2 digits). However, %SUBDT would always return the full year (4 digits) irrespective of the format of the date passed. 
    • Line - 17: %SUBDT with *ISO date format. 
    • Line - 22: %SUBDT with *DMY date format. 
  • We don't necessarily pass the date in a variable. 
    • Lines - 17 & 22: We are passing the date variables to extract the year. 
    • Line - 27: We are passing %DATE() to extract the month from the current date. 
    • Line - 32: We are passing the required date to extract the day from the passed date. 

Let's have a look at another example to extract Hours, Minutes and Seconds from a Time. 

Extract Hours, Minutes and Seconds from Time using %SUBDT - RPGLE

In the above example, 
  • Line - 17: We are using *HOURS full form for value instead of short form *H. Using any of these two would return the same result. 
  • Either a variable with time, %TIME() or a time literal can be passed to %SUBDT. 
    • Line - 15: A time variable is being passed to extract hours. 
    • Line - 20: %TIME() is being passed to extract minutes. 
    • Line - 25: Specific time can be passed directly by using time literal 't'. We are extracting seconds from the time passed. 

Let's have a look at the final example to extract the milli seconds by passing the Timestamp. We will also explore the two optional parameters in this example. 

Extract Seconds and Milliseconds from Timestamp using %SUBDT - RPGLE

Extracting Years, Months, Days, Hours and Minutes from Timestamp is same as extracting from Date and Time. Milliseconds can be extracted from Timestamp and optional parameters can be used on seconds to specify how many decimal positions are to be extracted for milliseconds. 

In the above example, 
  • Line - 14: Extracting milliseconds from timestamp. 
  • Line - 19: Extracting seconds from timestamp along with milliseconds by specifying optional parameters digits and number of decimal positions. 
    • We are passing digits (total) '6' and decimal positions '4'. %SUBDT would only return milliseconds up to 4 digits along with seconds. wSeconds variable has been defined with 6 decimal positions, so the last two decimal positions would be stored as '00'. 
    • If the third parameter (digits) is specified, Fourth parameter (decimal positions) must be specified. And, Fourth parameter should always be 2 less than third parameter (2 digits for seconds).
    • If optional parameters aren't specified, no milliseconds would be returned. 

If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form. 


  1. Can you organize and list the questions which you found difficult in interviews you have faced ?

    1. Sure Santosh. Thanks for the input, I will add a page with the Interview questions from my experience.


Post a Comment

Popular posts from this blog

All about READ in RPGLE & Why we use it with SETLL/SETGT?

READ READ is one of the most used Opcodes in RPGLE. As the name suggests main purpose of this Opcode is to read a record from Database file. What are the different READ Opcodes? To list, Below are the five Opcodes.  READ - Read a Record READC - Read Next Changed Record READE - Read Equal Key Record READP - Read Prior Record READPE - Read Prior Equal Record We will see more about each of these later in this article. Before that, We will see a bit about SETLL/SETGT .  SETLL (Set Lower Limit) SETLL accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Corresponding Record (or Next Record if exact match isn't found).  SETGT (Set Greater Than) SETGT accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Next Record (Greater Than the Key value). Syntax: SETLL SEARCH-ARGUMENTS/KEYFIELDS FILENAME SETGT  SEARCH-ARGUMENTS/KEYFIELDS FILENAME One of the below can be passed as Search Arguments. Key Fiel

What we need to know about CHAIN (RPGLE) & How is it different from READ?

CHAIN READ & CHAIN, These are one of the most used (& useful) Opcodes by any RPG developer. These Opcodes are used to read a record from file. So, What's the difference between CHAIN & READ?   CHAIN operation retrieves a record based on the Key specified. It's more like Retrieving Random record from a Database file based on the Key fields.  READ operation reads the record currently pointed to from a Database file. There are multiple Opcodes that start with READ and all are used to read a record but with slight difference. We will see more about different Opcodes and How they are different from each other (and CHAIN) in another article. Few differences to note.  CHAIN requires Key fields to read a record where as READ would read the record currently pointed to (SETLL or SETGT are used to point a Record).  If there are multiple records with the same Key data, CHAIN would return the same record every time. READE can be used to read all the records with the specified Ke