w w w . P r o v a t o S y s . c o m
M i c h a e l   C a t a l a n i
9 0 1 . 5 8 1 . 8 7 9 1

 

               


The Date_s service program is a collection of functions that handle date issues.

 

Self Contained Demo Program

The following program contains all prototypes and procedures copied directly into the program. All you have to do is to compile the display file, and then the program, and you will see the date functions in action.

 

DateDemoD  Display File

		
		
     A*%%TS  SD  20090311  155429  QPGMR       REL-V6R1M0  5761-WDS
     A*%%EC
     A                                      DSPSIZ(24 80 *DS3)
     A                                      CF03(03)
     A          R SCREEN01
     A*%%TS  SD  20090311  155429  QPGMR       REL-V6R1M0  5761-WDS
     A                                 23  2'F3-Exit'
     A                                      COLOR(WHT)
     A                                  1  2DATE
     A                                      EDTCDE(Y)
     A                                      COLOR(PNK)
     A                                  2  2TIME
     A                                      COLOR(PNK)
     A                                  1 70USER
     A                                      COLOR(PNK)
     A                                  1 21'Date Functions Demo Program'
     A                                      COLOR(BLU)
     A                                  4  2'The following functions are passed-
     A                                       the system date (%date), so the re-
     A                                      sults      will always be for the c-
     A                                      urrent day.'
     A                                  8  2'Date_DayOfTheWeek       '
     A                                      COLOR(PNK)
     A                                  7  2'Function Name    '
     A                                      COLOR(BLU)
     A                                      DSPATR(UL)
     A                                  7 28'Value Returned                  '
     A                                      COLOR(BLU)
     A                                      DSPATR(UL)
     A                                  9  2'Date_YearBegin          '
     A                                      COLOR(PNK)
     A                                 10  2'Date_YearEnd            '
     A                                      COLOR(PNK)
     A                                 11  2'Date_FirstOfTheMonth    '
     A                                      COLOR(PNK)
     A                                 12  2'Date_EndOfTheMonth      '
     A                                      COLOR(PNK)
     A                                 13  2'Date_DaysInYear         '
     A                                      COLOR(PNK)
     A                                 14  2'Date_DaysToString       '
     A                                      COLOR(PNK)
     A                                 15  2'Date_DaysToStringWithDay'
     A                                      COLOR(PNK)
     A                                 16  2'Date_DayOfTheYear       '
     A                                      COLOR(PNK)
     A            DAYNAME       10A  O  8 28COLOR(PNK)
     A            YEARBEGIN       L  O  9 28COLOR(PNK)
     A            YEAREND         L  O 10 28COLOR(PNK)
     A            MONTHBEGIN      L  O 11 28COLOR(PNK)
     A            MONTHEND        L  O 12 28COLOR(PNK)
     A            DAYSINYEAR     3Y 0O 13 28EDTCDE(Z)
     A                                      COLOR(PNK)
     A            DATESTR       30A  O 14 28COLOR(PNK)
     A            DATESTRD      30A  O 15 28COLOR(PNK)
     A            DAYOFYEAR      3Y 0O 16 28EDTCDE(Z)
     A                                      COLOR(PNK)                                                                                                                                                

DateDemo1 RPG Program 

		
       * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e D e m o 1   P r o g r a m
      *
      *          @copyrite 1997, 2009 Michael Catalani
      *           ProvatoSys  www.ProvatoSys.com
      *           mcatalani@aol.com
      *           901.581.8791
      *
      *  Demonstrates the Date_s service program functions
      *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

     h dftactgrp(*no)
     fDateDemoD cf   e             workstn


     d Date_DayOfTheWeek...
     d                 pr            10a
     d  PassedDate                     d   const

     d Date_YearBegin...
     d                 pr              d
     d  PassedDate                     d   const

     d Date_YearEnd...
     d                 pr              d
     d  PassedDate                     d   const

     d Date_FirstofTheMonth...
     d                 pr              d
     d  PassedDate                     d   const

     d Date_EndOfTheMonth...
     d                 pr              d
     d  PassedDate                     d   const

     d Date_DaysInYear...
     d                 pr             3p 0
     d  PassedDate                     d   const

     d Date_DateToString...
     d                 pr            30a   varying
     d  PassedDate                     d   const

     d Date_DateToStringWithDay...
     d                 pr            30a   varying
     d  PassedDate                     d   const

     d Date_DayOfTheYear...
     d                 pr             3s 0
     d  PassedDate                     d   const

      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e D e m o     Variables and Program Code
      *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

      /free


       dow NOT *in03;

         DayName = Date_DayOfTheWeek( %date );
         YearBegin =  Date_YearBegin( %date );
         YearEnd   =  Date_YearEnd( %date );
         MonthBegin =  Date_FirstOfTheMonth( %date );
         MonthEnd =  Date_EndOfTheMonth( %date );
         DaysInYear = Date_DaysInYear( %date );
         DateStr = Date_DateToString( %date );
         DateStrD = Date_DateToStringWithDay( %date );
         DayOfYear = Date_DayOfTheYear( %date );

         exfmt screen01;
         if *in03 = *on;
           leave;
         endif;

       enddo;

       *inlr = *on;
       return;

      /end-free

      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ D a y O f T h e W e e k
      *
      *          Returns the Day Name for a given date
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - String containing the Day Name.
      *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_DayOfTheWeek...
     p                 b                   export
     d Date_DayOfTheWeek...
     d                 pi            10a
     d
     d  PassedDate                     d   const

     d                 ds
     d  Day                          70a   inz('Monday    +
     d                                          Tuesday   +
     d                                          Wednesday +
     d                                          Thursday  +
     d                                          Friday    +
     d                                          Saturday  +
     d                                          Sunday   ')
     d  DayArray                     10a   overlay(Day) dim(7)
     d  DayNumber      s              1s 0

      /free

       // Get The Day Of Week
       daynumber = %rem( %diff( PassedDate : D'1899-12-31' : *days ) : 7 );

       if DayNumber = 0;
         DayNumber = 7;
       endif;

       return DayArray( DayNumber );

      /end-free
     p                 e

      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ Y e a r B e g i n
      *
      *          Returns the January 1 date for a given date
      *            ie  PassedDate = 2008-06-02
      *                Returns      2008-01-01
      *
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - January 1st Date For The year of the passed Date
      *                        ( Date Field )
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_YearBegin  b                   export
     d Date_YearBegin  pi              d
     d  PassedDate                     d   const

     d  YearBeginDate  s               d

      /free


        YearBeginDate = PassedDate -
                        %months( %subdt( PassedDate : *months ) - 1 ) -
                        %days(   %subdt( PassedDate : *days )   - 1 );

        return ( YearBeginDate );

      /end-free
     p                 e


      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ Y e a r E n d
      *
      *          Returns the December 31st date for a given date
      *            ie  PassedDate = 2008-06-02
      *                Returns      2008-12-31
      *
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - December 1st Date For The year of the passed Date
      *                        ( Date Field )
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


     p Date_YearEnd    b                   export
     d Date_YearEnd    pi              d
     d  PassedDate                     d   const

     d  YearEndDate    s               d

      /free

        YearEndDate = Date_YearBegin( PassedDate ) + %years( 1 ) - %days( 1 );

        return (YearEndDate);

      /end-free
     p                 e


      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ F i r s t O f T h e M o n t h
      *
      *        Returns the 1st day of the month in date format for a given date
      *            ie  PassedDate = 2008-06-10
      *                Returns      2008-06-01
      *
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - 1st Date For The Month of the passed Date
      *                        ( Date Field )
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_FirstOfTheMonth...
     p                 b                   export
     d Date_FirstOfTheMonth...
     d                 pi              d
     d  PassedDate                     d   const

     d FirstOfMonthDate...
     d                 s               d

      /free

       FirstOfMonthDate = PassedDate -
                          %days( %subdt( PassedDate : *d )) +
                          %days( 1 );

       return ( FirstOfMonthDate );

      /end-free
     p                 e


      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ E n d O f T h e M o n t h
      *
      *        Returns the last day of the month in date format for a given date
      *            ie  PassedDate = 2008-06-10
      *                Returns      2008-06-01
      *
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - 1st Date For The Month of the passed Date
      *                        ( Date Field )
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_EndOfTheMonth...
     p                 b                   export
     d Date_EndOfTheMonth...
     d                 pi              d
     d  PassedDate                     d   const

     d  EndOfMonthDate...
     d                 s               d

      /free

         EndOfMonthDate = Date_FirstOfTheMonth( PassedDate ) +
                          %months(1) -
                          %days(1);

        return ( EndOfMonthDate );      
        
      /end-free
     p                 e


      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ D a y s I n Y e a r
      *
      *          Get The Number Of Days In The Year Of A Passed Date
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - Days In The Year  ( 3,0 )
      *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_DaysInYear...
     p                 b                   export
     d Date_DaysInYear...
     d                 pi             3p 0
     d  PassedDate                     d   const

     d Midnight        s               t   inz
     d FromDate        s               z
     d ToDate          s               z
     d Days            s              3p 0

      /free

        FromDate = Date_YearBegin( PassedDate ) +  Midnight;
        ToDate   = Date_YearEnd( PassedDate ) + Midnight;

        Days = %diff( ToDate : FromDate : *days ) + 1;

        return (Days);

      /end-free
     p                 e

      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ D a t e T o S t r i n g
      *
      *          Takes a date field and converts it to a phrase
      *          ie:  2009-03-15 = March 15th, 2009
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - String Containing the Date Phrase
      *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_DateToString...
     p                 b                   export
     p
     d Date_DateToString...
     d                 pi            30a   varying
     d  PassedDate                     d   const

     d  DateString     s             30a   varying
     d  Day            s              2p 0
     d  EndDay         s              2p 0

     d                 ds
     d  MonthName                   120a   inz('January   +
     d                                          February  +
     d                                          March     +
     d                                          April     +
     d                                          May       +
     d                                          June      +
     d                                          July      +
     d                                          August    +
     d                                          September +
     d                                          October   +
     d                                          November  +
     d                                          December  ')
     d  MonthArray                   10a   overlay(MonthName) dim(12)

     d  st             c                   'st'
     d  nd             c                   'nd'
     d  rd             c                   'rd'
     d  th             c                   'th'
     d
     d                 ds
     d  DaySuffix                    20a   inz('st+
     d                                          nd+
     d                                          rd+
     d                                          th+
     d                                          th+
     d                                          th+
     d                                          th+
     d                                          th+
     d                                          th+
     d                                          th')
     d  DayArray                      2a   overlay(DaySuffix) dim(10)

      /free

       // Insert the month name
       DateString = %trim( MonthArray( %subdt( PassedDate : *months ) ) );

       //Insert the day
       Day = %subdt( PassedDate : *days );
       DateString = DateString + ' ' + %char( day );
       if day > 10 and day < 14;
         DateString = DateString + th;
       else;
         EndDay =  %int( %subst( %editc( day : 'X' ) : 2 : 1 ) );

         if EndDay = 0;
           EndDay = 10;
         endif;

         DateString = DateString + DayArray( EndDay );
       endif;

       //Insert the year
       DateString = DateString + ', ' + %char(%subdt( PassedDate : *years ));

       return ( DateString );

      /end-free
     p                 e


      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ D a t e T o S t r i n g W i t b D a y
      *
      *          Takes a date field and converts it to a phrase, starting with
      *          the name of the day.
      *
      *          ie:  2009-03-15 = Sunday, March 15th, 2009
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - String Containing the Date Phrase
      *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_DateToStringWithDay...
     p                 b                   export
     p
     d Date_DateToStringWithDay...
     d                 pi            30a   varying
     d  PassedDate                     d   const

     d  DateString     s             30a   varying

      /free

          DateString = %trim( Date_DayOfTheWeek( PassedDate ) )+
                       ', ' +
                       Date_DateToString( PassedDate );

          return ( DateString );

      /end-free
     p                 e


      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ D a y O f T h e Y e a r
      *
      *          Get The Day # of the Year
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - Day # Of The Year ( 3,0 )
      *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_DayOfTheYear...
     p                 b                   export
     d Date_DayOfTheYear...
     d                 pi             3s 0
     d  PassedDate                     d   const
     d DayNumber       s              3s 0
     d Midnight        s               t   inz
     d FromDate        s               z
     d ToDate          s               z
      /free


       FromDate = Date_YearBegin( PassedDate ) +  Midnight;
       ToDate   = PassedDate + Midnight;

       daynumber = %diff( ToDate : FromDate : *days) + 1 ;

       return DayNumber;

      /end-free
     p                 e                                                                                                                            

 

If you execute the DateDemo1 program, you will receive a screen that looks similar to the one below.

 

Functions In This Service Program

The Date_s service program contains the following functions:

Date_DayOfTheWeek Returns the day name
Date_YearBegin Returns the January 1st date for the date that was passed
Date_YearEnd Returns the December 31th date for the date that was passed
Date_FirstOfTheMonth Returns the 1st day of the month for the date that was passed
Date_EndOfTheMonth Returns the date of the last day of the month
Date_DaysInYear Returns the number of days in the year for the date passed
Date_DaysToString Returns the date in a word string format
Date_DaysToStringWithDay Returns the date in a word string format, with the day name included
Date_DayOfTheYear Returns the day number of the year

 

Date_P ProtoTypes

		

      /if defined(DATE_p)
      /eof
      /endif

      /define DATE_p

     d Date_DayOfTheWeek...
     d                 pr            10a
     d  PassedDate                     d   const

     d Date_YearBegin...
     d                 pr              d
     d  PassedDate                     d   const

     d Date_YearEnd...
     d                 pr              d
     d  PassedDate                     d   const

     d Date_FirstofTheMonth...
     d                 pr              d
     d  PassedDate                     d   const

     d Date_EndOfTheMonth...
     d                 pr              d
     d  PassedDate                     d   const

     d Date_DaysInYear...
     d                 pr             3p 0
     d  PassedDate                     d   const

     d Date_DateToString...
     d                 pr            30a   varying
     d  PassedDate                     d   const

     d Date_DateToStringWithDay...
     d                 pr            30a   varying
     d  PassedDate                     d   const

     d Date_DayOfTheYear...
     d                 pr             3s 0
     d  PassedDate                     d   const                                                   

 

Date_S Service Program Source

		
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D A T E _ S   S e r v i c e   P r o g r a m
      *
      *          @copyrite 1997, 2009 Michael Catalani
      *           ProvatoSys  www.ProvatoSys.com
      *           mcatalani@aol.com
      *           901.581.8791
      *
      *
      *  FUNCTION            DESCRIPTION
      *  DayOfTheWeek        Returns the day of the week name
      *  YearBegin           Returns The 1st Day Of The Year
      *  YearEnd             Returns the last day of the year
      *  FirstOfTheMonth     Returns the first day of the month date
      *  EndOfTheMonth       Returns the last day of the month date
      *  DaysInTheYear       Returns the number of days in the year
      *  DateToString        Returns the word phrase of a date
      *  DateToStringWithDay Returns the word phrase of a date with the day
      *  DayOfTheYear        The day # of the year
      *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

     h nomain

      /copy *libl/qrpglesrc,date_p


      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ D a y O f T h e W e e k
      *
      *          Returns the Day Name for a given date
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - String containing the Day Name.
      *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_DayOfTheWeek...
     p                 b                   export
     d Date_DayOfTheWeek...
     d                 pi            10a
     d
     d  PassedDate                     d   const

     d                 ds
     d  Day                          70a   inz('Monday    +
     d                                          Tuesday   +
     d                                          Wednesday +
     d                                          Thursday  +
     d                                          Friday    +
     d                                          Saturday  +
     d                                          Sunday   ')
     d  DayArray                     10a   overlay(Day) dim(7)
     d  DayNumber      s              1s 0

      /free

       // Get The Day Of Week
       daynumber = %rem( %diff( PassedDate : D'1899-12-31' : *days ) : 7 );

       if DayNumber = 0;
         DayNumber = 7;
       endif;

       return DayArray( DayNumber );

      /end-free
     p                 e

      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ Y e a r B e g i n
      *
      *          Returns the January 1 date for a given date
      *            ie  PassedDate = 2008-06-02
      *                Returns      2008-01-01
      *
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - January 1st Date For The year of the passed Date
      *                        ( Date Field )
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_YearBegin  b                   export
     d Date_YearBegin  pi              d
     d  PassedDate                     d   const

     d  YearBeginDate  s               d

      /free


        YearBeginDate = PassedDate -
                        %months( %subdt( PassedDate : *months ) - 1 ) -
                        %days(   %subdt( PassedDate : *days )   - 1 );

        return ( YearBeginDate );

      /end-free
     p                 e


      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ Y e a r E n d
      *
      *          Returns the December 31st date for a given date
      *            ie  PassedDate = 2008-06-02
      *                Returns      2008-12-31
      *
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - December 1st Date For The year of the passed Date
      *                        ( Date Field )
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


     p Date_YearEnd    b                   export
     d Date_YearEnd    pi              d
     d  PassedDate                     d   const

     d  YearEndDate    s               d

      /free

        YearEndDate = Date_YearBegin( PassedDate ) + %years( 1 ) - %days( 1 );

        return (YearEndDate);

      /end-free
     p                 e


      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ F i r s t O f T h e M o n t h
      *
      *        Returns the 1st day of the month in date format for a given date
      *            ie  PassedDate = 2008-06-10
      *                Returns      2008-06-01
      *
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - 1st Date For The Month of the passed Date
      *                        ( Date Field )
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_FirstOfTheMonth...
     p                 b                   export
     d Date_FirstOfTheMonth...
     d                 pi              d
     d  PassedDate                     d   const

     d FirstOfMonthDate...
     d                 s               d

      /free

       FirstOfMonthDate = PassedDate -
                          %days( %subdt( PassedDate : *d )) +
                          %days( 1 );

       return ( FirstOfMonthDate );

      /end-free
     p                 e


      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ E n d O f T h e M o n t h
      *
      *        Returns the last day of the month in date format for a given date
      *            ie  PassedDate = 2008-06-10
      *                Returns      2008-06-01
      *
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - 1st Date For The Month of the passed Date
      *                        ( Date Field )
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_EndOfTheMonth...
     p                 b                   export
     d Date_EndOfTheMonth...
     d                 pi              d
     d  PassedDate                     d   const

     d  EndOfMonthDate...
     d                 s               d

      /free

         EndOfMonthDate = Date_FirstOfTheMonth( PassedDate ) +
                          %months(1) -
                          %days(1);

        return ( EndOfMonthDate );      
        
      /end-free
     p                 e


      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ D a y s I n Y e a r
      *
      *          Get The Number Of Days In The Year Of A Passed Date
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - Days In The Year  ( 3,0 )
      *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_DaysInYear...
     p                 b                   export
     d Date_DaysInYear...
     d                 pi             3p 0
     d  PassedDate                     d   const

     d Midnight        s               t   inz
     d FromDate        s               z
     d ToDate          s               z
     d Days            s              3p 0

      /free

        FromDate = Date_YearBegin( PassedDate ) +  Midnight;
        ToDate   = Date_YearEnd( PassedDate ) + Midnight;

        Days = %diff( ToDate : FromDate : *days ) + 1;

        return (Days);

      /end-free
     p                 e

      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ D a t e T o S t r i n g
      *
      *          Takes a date field and converts it to a phrase
      *          ie:  2009-03-15 = March 15th, 2009
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - String Containing the Date Phrase
      *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_DateToString...
     p                 b                   export
     p
     d Date_DateToString...
     d                 pi            30a   varying
     d  PassedDate                     d   const

     d  DateString     s             30a   varying
     d  Day            s              2p 0
     d  EndDay         s              2p 0

     d                 ds
     d  MonthName                   120a   inz('January   +
     d                                          February  +
     d                                          March     +
     d                                          April     +
     d                                          May       +
     d                                          June      +
     d                                          July      +
     d                                          August    +
     d                                          September +
     d                                          October   +
     d                                          November  +
     d                                          December  ')
     d  MonthArray                   10a   overlay(MonthName) dim(12)

     d  st             c                   'st'
     d  nd             c                   'nd'
     d  rd             c                   'rd'
     d  th             c                   'th'
     d
     d                 ds
     d  DaySuffix                    20a   inz('st+
     d                                          nd+
     d                                          rd+
     d                                          th+
     d                                          th+
     d                                          th+
     d                                          th+
     d                                          th+
     d                                          th+
     d                                          th')
     d  DayArray                      2a   overlay(DaySuffix) dim(10)

      /free

       // Insert the month name
       DateString = %trim( MonthArray( %subdt( PassedDate : *months ) ) );

       //Insert the day
       Day = %subdt( PassedDate : *days );
       DateString = DateString + ' ' + %char( day );
       if day > 10 and day < 14;
         DateString = DateString + th;
       else;
         EndDay =  %int( %subst( %editc( day : 'X' ) : 2 : 1 ) );

         if EndDay = 0;
           EndDay = 10;
         endif;

         DateString = DateString + DayArray( EndDay );
       endif;

       //Insert the year
       DateString = DateString + ', ' + %char(%subdt( PassedDate : *years ));

       return ( DateString );

      /end-free
     p                 e


      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ D a t e T o S t r i n g W i t b D a y
      *
      *          Takes a date field and converts it to a phrase, starting with
      *          the name of the day.
      *
      *          ie:  2009-03-15 = Sunday, March 15th, 2009
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - String Containing the Date Phrase
      *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_DateToStringWithDay...
     p                 b                   export
     p
     d Date_DateToStringWithDay...
     d                 pi            30a   varying
     d  PassedDate                     d   const

     d  DateString     s             30a   varying

      /free

          DateString = %trim( Date_DayOfTheWeek( PassedDate ) )+
                       ', ' +
                       Date_DateToString( PassedDate );

          return ( DateString );

      /end-free
     p                 e


      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e _ D a y O f T h e Y e a r
      *
      *          Get The Day # of the Year
      *
      *          Input Field - Input Date ( Date Field )
      *          Returns     - Day # Of The Year ( 3,0 )
      *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     p Date_DayOfTheYear...
     p                 b                   export
     d Date_DayOfTheYear...
     d                 pi             3s 0
     d  PassedDate                     d   const
     d DayNumber       s              3s 0
     d Midnight        s               t   inz
     d FromDate        s               z
     d ToDate          s               z
      /free


       FromDate = Date_YearBegin( PassedDate ) +  Midnight;
       ToDate   = PassedDate + Midnight;

       daynumber = %diff( ToDate : FromDate : *days) + 1 ;

       return DayNumber;

      /end-free
     p                 e

                                                                                 

Date_S Binder Source

		
             STRPGMEXP  PGMLVL(*CURRENT)
                EXPORT     SYMBOL(Date_DayOfTheWeek)
                EXPORT     SYMBOL(Date_YearBegin)
                EXPORT     SYMBOL(Date_YearEnd)
                EXPORT     SYMBOL(Date_FirstOfTheMonth)
                EXPORT     SYMBOL(Date_EndOfTheMonth)
                EXPORT     SYMBOL(Date_DaysInYear)
                EXPORT     SYMBOL(Date_DateToString)
                EXPORT     SYMBOL(Date_DateToStringWithDay)
                EXPORT     SYMBOL(Date_DayOfTheYear)
             ENDPGMEXP
                                   

 

Creating The Service Program

CRTRPGMOD MODULE(CF/DATE_S) SRCFILE(CF/QRPGLESRC)

CRTSRVPGM SRVPGM(CF/DATE_S)

** replace the library "CF" with the library you are using.

Perform an ADDBNDDIRE  to place the service program into a binding directory. 

 

DateDemo Program

The following source is for the DateDemo program. It performs identically to the DateDemo1 program  at the top of this page,  but it is written using the Date_s service program instead of having the code and prototypes hard coded inside the program. 

		
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e D e m o   P r o g r a m
      *
      *          @copyrite 1997, 2009 Michael Catalani
      *           ProvatoSys  www.ProvatoSys.com
      *           mcatalani@aol.com
      *           901.581.8791
      *
      *  Demonstrates the Date_s service program functions
      *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

     h bnddir('CF') dftactgrp(*no)
     fDateDemoD cf   e             workstn

      /copy *libl/qrpglesrc,Date_p

      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      *
      *  D a t e D e m o     Variables and Program Code
      *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

      /free


       dow NOT *in03;

         DayName = Date_DayOfTheWeek( %date );
         YearBegin =  Date_YearBegin( %date );
         YearEnd   =  Date_YearEnd( %date );
         MonthBegin =  Date_FirstOfTheMonth( %date );
         MonthEnd =  Date_EndOfTheMonth( %date );
         DaysInYear = Date_DaysInYear( %date );
         DateStr = Date_DateToString( %date );
         DateStrD = Date_DateToStringWithDay( %date );
         DayOfYear = Date_DayOfTheYear( %date );

         exfmt screen01;
         if *in03 = *on;
           leave;
         endif;

       enddo;

       *inlr = *on;
       return;