• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • A Bevy of BIFs: Getting a Date is Easy with %Date

    February 18, 2009 Susan Gantner

    Almost every kind of business application uses dates. Hopefully you’re taking full advantage of all the RPG support for date data types to simplify your logic. If you’re using more than one line of code to calculate the number of days, months or years between two dates, or to add a number of days, months, or years to a date, you’re working way too hard!

    Many of the “dates” we deal with come from a screen or a database file and aren’t true date data type fields. They are numeric or character fields that contain values that we treat as dates. So to be able to use that data with functions like %Diff (to calculate the difference between two dates) or %Days (to add some number of days to a date), you need to convert the data to true date formats.

    That’s where %Date comes in. There are also similar functions for converting data into time and timestamp fields, but I’ll concentrate on dates in these examples. The syntax looks like this: %DATE{(expression{:date-format})}. The expression can be character or numeric (or sometimes a timestamp, although that’s rarely used). Because it is an expression, we can concatenate or add fields together if the year, month, and day are stored as separate fields.

    The second parameter, date format, is optional, but since the default is the default date format for the program, it is typically needed. Keep in mind that this is the format of the “date-like” expression in the first parameter–not the format you want the output to be. You could control the default date format using the DATFMT keyword on the H spec, but it’s probably easier and safer to specify it using the parameter.

    Some examples of using %Date follow. All result in output of the *ISO date value of “1988-06-21”.

    H  Option(*SrcStmt:*NoDebugIO)
    
    D Number          S              6S 0 inz(880621)
    D Character       S              6A   inz('880621')
    D DateFld         S               D
    D Day             S              2  0 Inz(21)
    D Month           S              2  0 Inz(6)
    D Year            S              2  0 Inz(88)
    D DayChar         S              2    Inz('21')
    D MonthChar       S              2    Inz('06')
    D YearChar        S              2    Inz('88')
    D JulianDay       S              3  0 Inz(173)
    D iAge            S              3  0  
    
    /FREE
    
       DateFld = %Date(Number:*YMD);
       DateFld = %Date(Character:*YMD0);
       DateFld = %Date((Year*10000)+ (Month*100)+ Day:*YMD);
       DateFld = %Date((YearChar + MonthChar + DayChar):*YMD0);
       DateFld = %Date((Year*1000)+ JulianDay :*JUL);
       DateFld = %Date('0' + Character:*CYMD0);
    

    One last hidden talent of %Date, which if you call it passing no parameters, it returns the current system date, is it is a nice way to get today’s date into an expression. Keep in mind that you may never need to define a date field in your program. Often you can simply convert your date-like data on the fly. See how simple it can be to use %Date compared to using the older SUBDUR support in this example:

              // Calculate Age of my favorite platform
       iAge = %Diff( %Date() : %Date(Number:*YMD) : *Years );
    

    What would happen, however, if an invalid date value were used as input to %Date? If the field Number contained zeroes or 880631 or 062188, then the first line of logic above would produce the error:

    RNQ0112 Date, Time or Timestamp value is not valid.
    

    So an important step in converting “date-like” data to real dates so that they can use other date functions involves either testing the date-like data first by using the Test operation code or using Monitor to capture and react to status code 112. I’ll cover the details of using Test and Monitor with date operations in a later tip. Stay tuned!

    Susan Gantner is one of the most respected System i gurus in the world and is one of the co-founders of System i Developer, an organization dedicated to RPG, DB2, and other relevant software technologies for the System i platform that hosts the new RPG & DB2 Summit conference. Gantner, who has worked in IBM’s Rochester and Toronto labs, left IBM to focus on training OS/400 and i5/OS shops on the latest programming technologies. She is also a regular speaker at COMMON and other user groups. Send your questions or comments for Susan to Ted Holt via the IT Jungle Contact page.

    RELATED STORY

    A Bevy of BIFs: Look Up to %LookUp



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    New Generation Software

    FREE Webinar:

    Creating Great Data for Enterprise AI

    Enterprise AI relies on many data sources and types, but every AI project needs a data quality, governance, and security plan.

    Wherever and however you want to analyze your data, adopting modern ETL and BI software like NGS-IQ is a great way to support your effort.

    Webinar: June 26, 2025

    RSVP today.

    www.ngsi.com – 800-824-1220

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Vision Solutions:  Learn About Data Integration for Business Intelligence
    COMMON:  Join us at the 2009 annual meeting and expo, April 26-30, Reno, Nevada
    WMCPA:  24rd Annual Spring Technical Conference, April 1 & 2, 2009, Delavan, WI

    IT Jungle Store Top Book Picks

    Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
    Getting Started with PHP for i5/OS: List Price, $59.95
    The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
    The System i Pocket RPG & RPG IV Guide: List Price, $69.95
    The iSeries Pocket Database Guide: List Price, $59.00
    The iSeries Pocket Developers' Guide: List Price, $59.00
    The iSeries Pocket SQL Guide: List Price, $59.00
    The iSeries Pocket Query Guide: List Price, $49.00
    The iSeries Pocket WebFacing Primer: List Price, $39.00
    Migrating to WebSphere Express for iSeries: List Price, $49.00
    iSeries Express Web Implementer's Guide: List Price, $59.00
    Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
    Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
    Getting Started with WebSphere Express for iSeries: List Price, $49.00
    WebFacing Application Design and Development Guide: List Price, $55.00
    Can the AS/400 Survive IBM?: List Price, $49.00
    The All-Everything Machine: List Price, $29.95
    Chip Wars: List Price, $29.95

    Stay-Linked Partners with Pragma for SSH Server Colonizing Endicott

    Leave a Reply Cancel reply

Volume 9, Number 6 -- February 18, 2009
THIS ISSUE SPONSORED BY:

Help/Systems
WorksRight Software
Guild Companies

Table of Contents

  • A Bevy of BIFs: Getting a Date is Easy with %Date
  • Mismatched Record Name Formats? No Problem!
  • Admin Alert: Solving i5/OS Inquiry Message Whodunits

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
  • Fresche Overhauls X-Analysis With Web UI, AI Smarts
  • Is It Time To Add The Rust Programming Language To IBM i?
  • Is IBM Going To Raise Prices On Power10 Expert Care?
  • IBM i PTF Guide, Volume 27, Number 20
  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle