• 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
    DRV Tech

    Get More Out of Your IBM i

    With soaring costs, operational data is more critical than ever. IBM shops need faster, easier ways to distribute IBM applications-based data to users more efficiently, no matter where they are.

    The Problem:

    For Users, IBM Data Can Be Difficult to Get To

    IBM Applications generate reports as spooled files, originally designed to be printed. Often those reports are packed together with so much data it makes them difficult to read. Add to that hardcopy is a pain to distribute. User-friendly formats like Excel and PDF are better, offering sorting, searching, and easy portability but getting IBM reports into these formats can be tricky without the right tools.

    The Solution:

    IBM i Reports can easily be converted to easy to read and share formats like Excel and PDF and Delivered by Email

    Converting IBM i, iSeries, and AS400 reports into Excel and PDF is now a lot easier with SpoolFlex software by DRV Tech.  If you or your users are still doing this manually, think how much time is wasted dragging and reformatting to make a report readable. How much time would be saved if they were automatically formatted correctly and delivered to one or multiple recipients.

    SpoolFlex converts spooled files to Excel and PDF, automatically emailing them, and saving copies to network shared folders. SpoolFlex converts complex reports to Excel, removing unwanted headers, splitting large reports out for individual recipients, and delivering to users whether they are at the office or working from home.

    Watch our 2-minute video and see DRV’s powerful SpoolFlex software can solve your file conversion challenges.

    Watch Video

    DRV Tech

    www.drvtech.com

    866.378.3366

    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

  • The Power11 Transistor Count Discrepancies Explained – Sort Of
  • Is Your IBM i HA/DR Actually Tested – Or Just Installed?
  • Big Blue Delivers IBM i Customer Requests In ACS Update
  • New DbToo SDK Hooks RPG And Db2 For i To External Services
  • IBM i PTF Guide, Volume 27, Number 33
  • Tool Aims To Streamline Git Integration For Old School IBM i Devs
  • IBM To Add Full System Replication And FlashCopy To PowerHA
  • Guru: Decoding Base64 ASCII
  • The Price Tweaking Continues For Power Systems
  • IBM i PTF Guide, Volume 27, Numbers 31 And 32

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