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

Pradeep Panga · May 3, 2021 · 4 min read

%SUBDT

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
  • Time - *HOURS, *MINUTES, *SECONDS
  • Timestamp - *DAYS, *MONTHS, *YEARS, *HOURS, *MINUTES, *SECONDS, *MSECONDS

Syntax:

%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
  • *MSECONDS - *MS

Let's have a look at couple of simple examples to see how this works.

E.g.:

Extract Date, Month and Year from a Date field.

**Free

// Standalone variables to store the Date
Dcl-S wDate Date(*ISO) ;
Dcl-S wDateDMY Date(*DMY) ;

// Standalone variables to store Year, Month and Date
Dcl-S wYear Packed(4 : 0) ;
Dcl-S wMonth Packed(2 : 0) ;
Dcl-S wDay Packed(2 : 0) ;

// Move current date to Date field
wDate = %Date() ; // 2021-05-02
wDateDMY = %Date() ; // 02/05/21

// Extract Year from Date
wYear = %SubDt(wDate : *Y) ;
Dsply wYear ; // 2021

// Year returned would always be 4 digits
// irrespective of the date format used
wYear = %SubDt(wDateDMY : *Y) ;
Dsply wYear ; // 2021

// Extract Month from Date
// %Date() can also be used with %SubDt
wMonth = %SubDt(%Date() : *M) ;
Dsply wMonth ; // 05

// Extract Day from Date
// Date can also be specified directly
wDay = %SubDt(d'2021-05-02' : *D) ;
Dsply wDay ; // 02

*InLr = *On ;
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.

**Free

// Standalone variables to store the Time
Dcl-S wTime Time ;

// Standalone variables to store Hours, Minutes and Seconds
Dcl-S wHours Packed(2 : 0) ;
Dcl-S wMinutes Packed(2 : 0) ;
Dcl-S wSeconds Packed(2 : 0) ;

// Move current time to Time field
wTime = %Time() ; // 23:08:10

// Extract Hours from Time
wHours = %SubDt(wTime : *HOURS) ;
Dsply wHours ; // 23

// Extract Minutes from Time
// %Time() can also be used with %SubDt
wMinutes = %SubDt(%Time() : *MN) ;
Dsply wMinutes ; // 08

// Extract Seconds from Time
// Date can also be specified directly
wSeconds = %SubDt(t'23.08.55' : *S) ;
Dsply wSeconds ; // 55

*InLr = *On ;
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.

**Free

// Standalone variables to store the Timestamp
Dcl-S wTimeStamp TimeStamp ;

// Standalone variables to store Hours, Minutes and Seconds
Dcl-S wMilliSeconds Packed(6 : 0) ;
Dcl-S wSeconds Packed(8 : 6) ;

// Move current timestamp to Timestamp field
wTimeStamp = %TimeStamp() ; // 2021-05-02-23.24.50.990546

// Extract Milliseconds from Timestamp
wMilliSeconds = %SubDt(wTimeStamp : *MS) ;
Dsply wMilliSeconds ; // 990546

// Extract Seconds from TimeStamp
// Timestamp can also be specified directly
wSeconds = %SubDt(z'2021-05-02-23.24.50.990546' : *S : 6 : 4) ;
Dsply wSeconds ; // 50.990500

*InLr = *On ;
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.

Keep reading

More from IBM i →