Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Conversion

Status
Not open for further replies.

jsplice

Programmer
Jun 3, 2003
88
0
0
US
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?
 
%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.

 
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.
 
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)

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

Date8 = %int(%char(%Date(): *iso0));
 
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.

 
If you are at V5R3, its even easier

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

No %char needed - the %dec can handle date fields
 
*=====================================================================
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
 
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)
 
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!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top