ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Return a month's name

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Return a month's name

    Hello, i was wondering if there is some built in function to get a month's name, using the month's number as a parameter.

    Current we use one of the following:

    -Use an inline table at the end of the program.
    -Have a built in table in a file, using chain to find the month
    -Built in data structure and overlay

    Is there something like name = month(1); to return the name? I tried to look inside the ile reference to no avail.

  • #2
    Re: Return a month's name

    RPG does not provide an built-in-function. You have to write your own one.

    If you want to determine the month's name from a date, you can use the SQL scalar function MONTHNAME.

    PHP Code:
      Exec SQL  Set :MonName MonthName(:MyDate); 
    If you only know the month number, you may built a dummy date and use it in combination with the scalar function monthname:
    PHP Code:
     Exec SQL
          set 
    :MonName =  MonthName('2008-' concat  Digits(Cast(:MyNumMon as Dec(20))) 
                                    
    concat '-01'
    National language considerations:
    The name of the month that is returned is based on the language used for messages in the job. This name of the month is retrieved from message CPX3BC0 in message file QCPFMSG in library *LIBL.
    Birgitta

    Comment


    • #3
      Re: Return a month's name

      Brigitta is always right when it comes to sql....

      If you interested in any of the RPG ways to do this

      Code:
            *-----------------------------------------
            * %MSeconds %Seconds, %Minutes, %Hours,
            * %Days, %Months, and %Years.
            * %Date, %Time, and %TimeStamp
            *-----------------------------------------
            *-----------------------------------------
            * Program Info
            *-----------------------------------------
           d                SDS
           d  @PGM                 001    010
           d  @PARMS               037    039  0
           d  @JOB                 244    253
           d  @USER                254    263
           d  @JOB#                264    269  0
            *
            *  Field Definitions.
            *
           d ISOdate         S               D
           d USAdate         S               D   DatFmt(*USA)
           d XMASDate        S               D   Inz(D'2003-12-25')
           d LogonDate       S               D
           d Date_Start      S             15
           d MonthNames      S             12    Dim(12) CtData
           d Date_String     S             40
      
            *
            * Time Stamp
            *
           dTimeStamp        S               Z
            *
           d WorkISO         S               D
           d Month           S              2  0
           d Day             S              2  0
           d Year            S              4  0
           d Decimal8        S              8  0
           d LogMonth        S              2  0
           d LogDay          S              2
           d LogDays         S              3  0
           d LogYear         S              4  0
           d NextMonth       S               D
           d EndOfMonth      S               D
            *
            * Variables for free RPG example + some above
            *
           d DateIn          S               D
           d FromISO         S               D
           d ToISO           S               D
           d DiffDays        S              3  0
           d WorkField       S              5  0
           d Name            S              9    Based(NamePtr)
           d Name2           S              9
           d NamePtr         S               *   Inz(%ADDR(Names))
           d Names           S             63    Inz('Sunday   Monday   Tuesday  Wedn+
           d                                     esdayThursday Friday   Saturday ')
            *
            * RPG-defined date formats and separators for Date data type
            * 2-Digit Year Formats
            * *MDY  Month/Day/Year  mm/dd/yy  8  09/26/03
            * *DMY  Day/Month/Year  dd/mm/yy  8  26/09/03
            * *YMD  Year/Month/Day  yy/mm/dd  8  03/09/26
            * *JUL  Julian          yy/ddd    6  03/926
      
            * 4-Digit Year Formats
            * *ISO  Int Standards Org yyyy-mm-dd  10  2003-09-26
            * *USA  IBM USA Standard  mm/dd/yyyy  10  09/26/2003
            * *EUR  IBM European Std  dd.mm.yyyy  10  26.09.2003
            * *JIS  Japan Indst Std   yyyy-mm-dd  10  2003-09-26
      
            * Okay first lets get todays date
            * For display purposes the date is now
            *      Friday September 26th 2003
            *  so date now looks like this 2003-09-26
            *  because the default date type is *ISO
            *
           C                   Eval      ISOdate = %Date()
            *
            *  Now that I have the date in a date format
            *  (*ISO) I can do stuff to it.
            *  Once I move this date to a decimal 8,0 field
            *  the date is now in format 20030926
            *  Not very exciting..yet
            *
           C                   Move      ISODate       Decimal8
            *
            *  Now back to the *ISO date  lets add
            *  1 month to the date.
            *  date after will equal 2003-10-26
            *  %days and % years works the same as %months
            *
           C                   Eval      WorkISO  = ISODate + %Months(1)
            *
            *  Logon date is set equal to today then the month is extracted
            *  the "*M" is the same as "*Months"  LogMonth = 09.
            *                                     LogDay   = 26.
            *
           C                   Eval      LogonDate =  %Date()
           C                   Extrct    LogonDate:*Y  LogYear
           C                   Extrct    LogonDate:*M  LogMonth
           C                   Extrct    LogonDate:*D  LogDay
      
               //                  --or--
      
            /free
      
                   LogYear  = %subdt(%date():*years);
                   LogMonth = %subdt(%date():*months);
                   LogDays  = %subdt(%date():*days);
      
                   //keep leading *zeros
                   logday = %editc(logdays:'X');
      
                   //lose leading *zeros
                   logday = %char(logdays);
      
            /end-free
      
            *
            *  Build the date string - Later we will add the day name
            *
           C                   Eval      Date_String =
           C                              %Trim(MonthNames(LogMonth))
           C                              + ' ' +  %Trim(LogDay)
           C                              + ' , '  + %Char(LogYear)
            *
            *
            * TimeStamp = yyyy-mm-dd-hh.mm.ss.mmmmmm (length 26).
            * TimeStamp = '2003-09-26-15.16.26.531000'
            *
           C                   Eval      TimeStamp = %TimeStamp()
            *
            *  Free Format date stuff   By the way Name2 = 'Friday'
            *
            /Free
              DateIn   = %Date()                     ;
              ISODate  = %Date()                     ;
              ISODate  = DateIn                      ;
              Year     = %Subdt(ISODate:*Y)          ;
              Month    = %Subdt(ISODate:*M)          ;
              Day      = %Subdt(ISODate:*D)          ;
              FromISO  = ISODate - %YEARS(1)         ;
              ToISO    = ISODate                     ;
              DiffDays = %Diff(ToISO:FromISO:*DAYS)  ;
              ISODate  = DateIn                      ;
      
              WorkField = %Diff(ISODate:D'1899-12-31':*DAYS);
              WorkField = %REM(WorkField:7);
      
              NamePtr = NamePtr + (WorkField * 9);
              Name2 = Name;
            /End-Free
            *
            *  Build the date string - With The Day Name
            *  DATE_STRING = 'Friday  September 26, 2003              '
            *
           C                   Eval      Date_String =
           C                              %trim(Name) + '  '
           C                              + %trim(MonthNames(LogMonth))
           C                              + ' '  + %Trim(LogDay)
           C                              + ', ' + %Char(LogYear)
            *
            * Calculate the last day of the month
            * ENDOFMONTH = '2003-09-30'
            *
           C     ISODate       AddDur    1:*Months     NextMonth
            /free
      
                Nextmonth = Isodate + %months(1);
      
            /end-free
           C                   Extrct    NextMonth:*D  DiffDays
           C     NextMonth     SubDur    DiffDays:*D   EndOfMonth
      
      
           C                   Eval      *INLR = *On
           C*----------------------------------------------------
      ** CTDATA MonthNames
      January
      February
      March
      April
      May
      June
      July
      August
      September
      October
      November
      December


      All my answers were extracted from the "Big Dummy's Guide to the As400"
      and I take no responsibility for any of them.

      www.code400.com

      Comment


      • #4
        Re: Return a month's name

        Yo Jamie, to make it even longer, you could put in a call to an API, get the language of the job, and make the program multilingual. i.e. more lines = more money
        Regards

        Kit
        http://www.ecofitonline.com
        DeskfIT - ChangefIT - XrefIT
        ___________________________________
        There are only 3 kinds of people -
        Those that can count and those that can't.

        Comment


        • #5
          Re: Return a month's name

          Short programs R4 sissies!
          All my answers were extracted from the "Big Dummy's Guide to the As400"
          and I take no responsibility for any of them.

          www.code400.com

          Comment


          • #6
            Re: Return a month's name

            as in Birgitta's code there is also a scalar function for DayName.
            Regards

            Kit
            http://www.ecofitonline.com
            DeskfIT - ChangefIT - XrefIT
            ___________________________________
            There are only 3 kinds of people -
            Those that can count and those that can't.

            Comment

            Working...
            X