×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Date Conversion

Date Conversion

Date Conversion

(OP)
I'm having a problem converting a date in the ISO format into a numeric variable.  Here is the declaration of the varaible along with the code used to get the current date:

D CURDATE         S              8  0

C                   EVAL      CURDATE = %UNS(%CHAR(%DATE():*ISO))

For some reason I get the following error:

"A character representation of a numeric value is in error."

I also tried using the %INT function and this gave me the same error.  Can anyone tell me what the problem is here?

RE: Date Conversion

%UNS takes a numeric expression, not a character expression.

If you are at V5R2 or higher, use %DEC instead of %UNS. Prior to that, you either have to use MOVE to get the character date from %DATE() to a numeric field, or write your own function in a service program to do the same thing.

Here is the service program we use.

Dic mihi solum facta, domina.

RE: Date Conversion

You don't need to use Move or create a service program.  Assuming your 8 digit date should be in yyyymmdd format, the following will work.

Date8 = %int(%char(DateIso : *iso0));

Notice the "0" following "*iso"

Date8 is an 8 digit numeric field and DateIso is a date field in ISO format.

RE: Date Conversion

(OP)
Thanks for the responses.  I ended up finding a date conversion "cheat sheet" that recommended something similar to this:

CURDATE = %DEC(%CHAR(%DATE():*ISO0):8:0)

RE: Date Conversion

Of course, if you want the system date then the following will do:

Date8 = %int(%char(%Date(): *iso0));

RE: Date Conversion

AlanHouston:

I was not assuming that jsplice was on V5R2, prior to which %INT does not convert character to numeric - that's why I included the service program link and the MOVE. Lots of places (I work at one of them) are still at V5R1.

I had a need to convert character amounts in a vendor file to numeric amounts at V5R1, and so we created that service program. It turns out that it's actually more flexible than %INT at V5R3, so I'll probably keep using it even after we upgrade. The advantage is that the service program will ignore the non-numeric stuff, except for a decimal point or minus sign, and will never give a run-time error.

Dic mihi solum facta, domina.

RE: Date Conversion

If you are at V5R3, its even easier

CURDATE = %DEC(%DATE():*ISO)

No %char needed - the %dec can handle date fields

RE: Date Conversion

*=====================================================================
     d monday          c                   d'1900-01-01'
*=====================================================================
     d d_o_w           s              1s 0 inz
     d d_o_y           s              3s 0 inz
     d qtr_nbr         s              1s 0 inz
     d wk_nbr          s              3s 0 inz
*=====================================================================
     d jan_4_dat       s               d   inz
     d jan_4_dow       s              1s 0 inz
*====================================================================
     d d_dat           s               d   inz
     d wk1_dat         s               d   inz
*====================================================================
     d n_iso           s              8s 0 inz
     d n_usa           s              8s 0 inz
     d n_eur           s              8s 0 inz
     d n_jis           s              8s 0 inz
     d n_longjul       s              7s 0 inz
      *
     d n_ymd           s              6s 0 inz
     d n_mdy           s              6s 0 inz
     d n_dmy           s              6s 0 inz
     d n_jul           s              5s 0 inz
      *
     d n_cymd          s              7s 0 inz
     d n_cmdy          s              7s 0 inz
     d n_cdmy          s              7s 0 inz
*=====================================================================
     d a_iso           s             10a   inz
     d a_usa           s             10a   inz
     d a_eur           s             10a   inz
     d a_jis           s             10a   inz
     d a_longjul       s              8a   inz
      *
     d a_ymd           s              8a   inz
     d a_mdy           s              8a   inz
     d a_dmy           s              8a   inz
     d a_jul           s              6a   inz
      *
     d a_cymd          s              9a   inz
     d a_cmdy          s              9a   inz
     d a_cdmy          s              9a   inz

      *=====================================================================
     d bom_d_dat       s               d   inz
     d boq_d_dat       s               d   inz
     d boy_d_dat       s               d   inz

      *====================================================================
     d eom_d_dat       s               d   inz
     d eoq_d_dat       s               d   inz
     d eoy_d_dat       s               d   inz

      *====================================================================
      /free
       // Current Date to Date Field
          d_dat = %date();

       // Date to Numeric-ISO
          n_iso = %dec( %char(d_dat : *iso0) : 8 : 0);

       // Date to Alpha-ISO
          a_iso = %char(d_dat : *iso);

       // Date to Numeric-USA
          n_usa = %dec( %char(d_dat : *usa0) : 8 : 0);

       // Date to Alpha-USA
          a_usa = %char(d_dat : *usa);

       // Date to Numeric-EUR
          n_eur = %dec( %char(d_dat : *eur0) : 8 : 0);

       // Date to Alpha-EUR
          a_eur = %char(d_dat : *eur);

       // Date to Numeric-JIS
          n_jis = %dec( %char(d_dat : *jis0) : 8 : 0);

       // Date to Alpha-JIS
          a_jis = %char(d_dat : *jis);


       // Date to Numeric-LONGJUL
          n_longjul = %dec( %char(d_dat : *longjul0) : 7 : 0);

       // Date to Alpha-LONGJUL
          a_longjul = %char(d_dat : *longjul);

       // Date to Numeric-YMD
          n_ymd = %dec( %char(d_dat : *ymd0) : 6 : 0);

       // Date to Alpha-YMD
          a_ymd = %char(d_dat : *ymd);

       // Date to Numeric-MDY
          n_mdy = %dec( %char(d_dat : *mdy0) : 6 : 0);

       // Date to Alpha-MDY
          a_mdy = %char(d_dat : *mdy);

       // Date to Numeric-DMY
          n_dmy = %dec( %char(d_dat : *dmy0) : 6 : 0);

       // Date to Alpha-DMY
          a_dmy = %char(d_dat : *dmy);

       // Date to Numeric-JUL
          n_jul = %dec( %char(d_dat : *jul0) : 5 : 0);

       // Date to Alpha-JUL
          a_jul = %char(d_dat : *jul);

       // Date to Numeric-CYMD
          n_cymd = %dec( %char(d_dat : *cymd0) : 7 : 0);

       // Date to Alpha-CYMD
          a_cymd = %char(d_dat : *cymd);

       // Date to Numeric-CMDY
          n_cmdy = %dec( %char(d_dat : *cmdy0) : 7 : 0);

       // Date to Alpha-CMDY
          a_cymd = %char(d_dat : *cymd);

       // Date to Numeric-CMDY
          n_cmdy = %dec( %char(d_dat : *cmdy0) : 7 : 0);

       // Date to Alpha-CMDY
          a_cmdy = %char(d_dat : *cmdy);

       // Date to Numeric-CDMY
          n_cdmy = %dec( %char(d_dat : *cdmy0) : 7 : 0);

       // Date to Alpha-CDMY
          a_cdmy = %char(d_dat : *cdmy);

       //============================
       // Numeric-ISO to Numeric-USA
          n_usa = %dec( %char( %date(n_iso : *iso) : *usa0) : 8 : 0);

       // Numeric-USA to Numeric-ISO
          n_iso = %dec( %char( %date(n_usa : *usa) : *iso0) : 8 : 0);

       // Numeric-ISO to Alpha-USA
          a_usa = %char( %date(n_iso : *iso) : *usa);

       // Alpha-USA to Numeric-ISO
          n_iso = %dec( %char( %date(a_usa : *usa) : *iso0) : 8 : 0);

       // Numeric-ISO to Alpha-MDY
          a_mdy = %char( %date(n_iso : *iso) : *mdy);

       // Alpha-MDY to Numeric-ISO
          n_iso = %dec( %char( %date(a_mdy : *mdy) : *iso0) : 8 : 0);

       //===========================================================

       // Current Date to Date Field
          d_dat = %date();

       // Current Date to Numeric-USA
          n_usa = %dec( %char( %date() : *usa0) : 8 : 0);

       // Current Date to Numeric-ISO
          n_iso = %dec( %char( %date() : *iso0) : 8 : 0);

       //==========================================================

       // Beginning of Month Date to Date Field
              bom_d_dat = d_dat - %days( %subdt(d_dat : *days) - 1);

       //===========================================================

       // End of Month Date to Date Field
              eom_d_dat = bom_d_dat + %months(1) - %days(1);

       //=============================================================

       // Beginning of Quarter Date to Date Field
          boq_d_dat = bom_d_dat - %months( %rem( %subdt(d_dat : *months) -1
          :3));

       //===============================================================

       // End of Quarter Date to Date Field
          eoq_d_dat = boq_d_dat + %months(3) - %days(1);

       //===================================================================

       // Beginning of Year Date to Date Field
          boy_d_dat = %date( %char( %subdt(d_dat : *years)) + '-01-01' :
          *iso);
       //===================================================================

       // Quarter Number
          qtr_nbr = %subdt(eoq_d_dat : *months) / 3;

       //===================================================================

       // Day of Week (1=Monday...7=Sunday)
          d_o_w = %rem( %diff(d_dat : monday : *days) : 7) + 1;

          if d_o_w <= *zeros;
            d_o_w += 7;
          endif;

       //===================================================================
       // Day of Year
          d_o_y = %diff( d_dat : boy_d_dat : *days) + 1;

       //===================================================================

       // Week Number (ISO 8601)
          jan_4_dat = %date( %char( %subdt(d_dat : *years)) + '-01-04'
          : *iso);
          jan_4_dow = %rem( %diff(jan_4_dat : monday : *days) : 7) + 1;

          wk_nbr = %div(d_o_y + jan_4_dow + 2 : 7);

          if wk_nbr = *zeros;
            wk_nbr = 53;
          endif;
       //===================================================================

        *inLR = *on;
        Return;

      /end-free

RE: Date Conversion

Hey flapeyre, you should think yourself lucky - I worked at a customer in November that was still on V3R2!!!

PeteJ
(Contract Code-monkey)

It's amazing how many ways there are to skin a cat
(apologies to the veggies)

RE: Date Conversion

V3R2?? The company I'm working at now got a new V3R6 box 10 years ago next month (just a couple of months before I was hired).

Anyway, this place uses CYYMMDD dates (like JDE). So I wrote some callable routines for Y2K in RPG 4. I have since put them all into service programs so that as I convert applications to ILE, I can replace them with the functions and speed things up a bit (the old programs have to be called with a parameter to set on *INLR).

For example:

CODE

FL01  /////////////////////////////////////////////////////////////////////////
FL01  //       Procedure: DaysDiffCYMDN7                                     //
FL01  //     Description: Calculates the number of days between              //
FL01  //     Startdate7 and Enddate7. Returns days as 7P0.                   //
FL01  /////////////////////////////////////////////////////////////////////////
FL01 PArvdates_...                                                             
FL01 P Daysdiffcymdn7  B                   Export                              
                                                                               
FL01 DArvdates_...                                                             
FL01 D Daysdiffcymdn7  PI             7P 0                                     
FL01 D Startdate7                     7P 0 Const                               
FL01 D Enddate7                       7P 0 Const                               
                                                                               
FL01  /Free                                                                    
FL01   Test(De) *CYMD Startdate7;                                              
FL01   If %ERROR;                                                              
FL01     Return *ZERO;                                                         
FL01   Endif;                                                                  
FL01   Test(De) *CYMD Enddate7;    
FL01   If %ERROR;                                                        
FL01     Return *ZERO;                                                   
FL01   Endif;                                                            
FL02   Monitor;                                                          
FL01   Return %DIFF(%DATE(Enddate7 : *CYMD) : %DATE(Startdate7 : *CYMD) :
FL01   *D);                                                              
FL02   On-Error;                                                         
FL02   Return *ZERO;                                                     
FL02   Endmon;                                                           
                                                                         
FL01  /End-Free                                                          
FL01 PArvdates_...                                                       
FL01 P Daysdiffcymdn7  E                               

The prototype:

CODE

FL02  /IF NOT DEFINED(Arvdates_DaysDiffCYMDN7_DONT_COPY)
FL02  /DEFINE Arvdates_DaysDiffCYMDN7_DONT_COPY       
FL02  /////////////////////////////////////////////////////////////////////////
FL02  //       Procedure: DaysDiffCYMD                                       //
FL02  //     Description: Calculates the number of days between              //
FL02  //     Startdate7 and Enddate7. Returns days as 7P0.                   //
FL02  /////////////////////////////////////////////////////////////////////////
FL02 DArvdates_...                       
FL02 D Daysdiffcymdn7  PR             7P 0
FL02 D Startdate7                     7P 0 Const
FL02 D Enddate7                       7P 0 Const
FL02  /ENDIF                                               

Feles mala! Cur cista non uteris? Stramentum novum in ea posui!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close