|
Hi Jeff,
Since you can have a variety of date formats to check you may want to
investigate writing up your own function to validate the date.
You can use the Convert Date to Lilian Format (CEEDAYS) API to determine
if the date is valid then use the Convert Lilian Date to Character Format
(CEEDATE) API to convert the Lilian date to whatever format you desire.
The CEE date APIs may be found within the ILE CEE APIs are the Info Center.
Basics are as follows...
//-------------------------------------------------------------
// Check and Convert a date
//
// 0 = A error occurred so the date is not returned
// date in CCYYMMDD = The date is valid so a date is returned
//
// You pass in a date. If there are no errors the converted
// date is returned.
//
//-------------------------------------------------------------
D chkAndCvtADate PR 8S 0
D datetocvt 255 OPTIONS(*VARSIZE)
D datetocvt S 255 OPTIONS(*VARSIZE)
/free
// Your process to receive the data goes here
// Check and convert the date
EpHOrDate = chkAndCvtADate(Start_Date);
If EpHorDate = 0;
// handle the error
Endif;
// Finish your processing
/end-free
//-------------------------------------------------------------
// Check and Convert a date
//
// 0 = A error occurred so the date is not returned
// date in CCYYMMDD = The date is valid so a date is returned
//
// You pass in a date. If there are no errors the converted
// date is returned.
//
//-------------------------------------------------------------
P chkAndCvtADate B
D chkAndCvtADate PI 8S 0
D entryDate 255 OPTIONS(*VARSIZE)
D dateToReturnDS DS
D dateToReturn 8S 0
D dateCharFmt 8 overlay(dateToReturn:1)
//------------------------------------------------
// CEEDAYS (Convert character date to lilian date)
//
//------------------------------------------------
D CEEDAYS PR EXTProc('CEEDAYS') OPDESC
D datetocvt 255 OPTIONS(*VARSIZE)
D datetocvtfmt 255 OPTIONS(*VARSIZE)
D liliandate 10I 0
D errorcode like(fc)
D datetocvt S 255 OPTIONS(*VARSIZE)
D datetocvtfmt S 255 OPTIONS(*VARSIZE)
D liliandate S 10I 0
D fc DS
D fc_Condition 4
D fc_Condition1 2B 0 overlay(fc_Condition:1)
D fc_Condition2 2B 0 overlay(fc_Condition:*NEXT)
D fc_bits 1
D fc_Facility 3
D fc_I_S_Info 4
D fc_MsgSev 2B 0
D fc_MsgNbr 2B 0
//---------------------------------------------
// CEEDATE (Convert lilian date character date)
//---------------------------------------------
D CEEDATE PR EXTProc('CEEDATE') OPDESC
D liliandate 10I 0
D datetocvtfmt 255 OPTIONS(*VARSIZE)
D converteddate 255 OPTIONS(*VARSIZE)
D errorcode like(fc)
D converteddate s 255 OPTIONS(*VARSIZE)
/free
//---------------------
// Initialize variables
//---------------------
Datetocvt = entryDate;
dateToReturn = 0;
//--------------------------------------------
// Convert from mm/dd/yyyy date to lilian date
//--------------------------------------------
datetocvtfmt = 'MM/DD/YYYY';
liliandate = 0;
Clear fc;
callP CEEDAYS(wrkFrmDate:datetocvtfmt:liliandate:fc);
if (fc_Condition = x'00000000'); // no error so convert lilian
to *USA
datetocvtfmt = 'YYYYMMDD';
convertedate = *Blanks;
callP CEEDATE(liliandate:datetocvtfmt:converteddate:fc);
dateCharFmt = converteddate;
return dateToReturn;
endif;
//--------------------------------------------
// Convert from dd/mm/yyyy date to lilian date
//--------------------------------------------
datetocvtfmt = 'DD/MM/YYYY';
liliandate = 0;
Clear fc;
callP CEEDAYS(wrkFrmDate:datetocvtfmt:liliandate:fc);
if (fc_Condition = x'00000000'); // no error so convert lilian
to *USA
datetocvtfmt = 'YYYYMMDD';
convertedate = *Blanks;
callP CEEDATE(liliandate:datetocvtfmt:converteddate:fc);
dateCharFmt = converteddate;
return dateToReturn;
endif;
//--------------------------------------------
// Convert from yyyy/mm/dd date to lilian date
//--------------------------------------------
datetocvtfmt = 'YYYY/MM/DD';
liliandate = 0;
Clear fc;
callP CEEDAYS(wrkFrmDate:datetocvtfmt:liliandate:fc);
if (fc_Condition = x'00000000'); // no error so convert lilian
to *USA
datetocvtfmt = 'YYYYMMDD';
convertedate = *Blanks;
callP CEEDATE(liliandate:datetocvtfmt:converteddate:fc);
dateCharFmt = converteddate;
return dateToReturn;
endif;
//----------------------------------------------------
// Return a 0 date since the format is not recognized
//----------------------------------------------------
return dateToReturn;
/end-free
P chkAndCvtADate B
The above function can be adapted to suit whatever your need.
HTH,
Gary Monnier
-----Original Message-----
From: rpg400-l-bounces@xxxxxxxxxxxx [mailto:rpg400-l-bounces@xxxxxxxxxxxx]
On Behalf Of Jeff Young
Sent: Wednesday, May 30, 2012 5:51 AM
To: rpg400-l@xxxxxxxxxxxx
Subject: Date Validation
All,
I have an application that needs to validate and convert a date from an
external system.
The format of the date can be m/d/ccyy,mm/d/ccyy,m/dd/ccyy,mm/dd/ccyy
with the "/" included.
The converted format needs to be a numeric field in the format ccyymmdd.
I have the following code in my program:
<snip>
// Edit Start Date
Monitor;
EpHOrDate = %Dec(%Date(Start_Date : *USA));
On-Error;
Load_Exception ('Start date is not a valid date');
EndMon;
// Edit Invoice Date
Monitor;
EpHInDate = %Dec(%Date(Invoice_Date : *USA));
On-Error;
Load_Exception('Invoice date is not a valid date');
EndMon;
</snip>
Where Start_Date and Invoice_Date are defined as 256A Varying and
EpHOrDate and EpHInDate are defined as 8p 0.
For values of 5/7/2012 for order date and 5/14/2012 for invoice date, the
code above rejects the order date, but accepts the invoice date.
I created a small test program to check the date editing process.
H Debug
D Date_in S 256A Varying
D Date_Out S 8 0
D Err S N
/Free
Monitor;
Date_Out = %Dec(%Date(Date_In : *USA));
On-Error;
Err = *On;
EndMon;
Return;
Using the values above, I set Date_In in DEBUG prior to the Monitor to
each of the dates.
BOTH of the dates were flagged as errors.
A. What am I doing wrong?
B. How can I handle the different date formats for editing and conversion?
The client is on V6R1M1
Thanks,
--
Jeff Young
Sr. Programmer Analyst
--
This is the RPG programming on the IBM i / System i (RPG400-L) mailing
list To post a message email: RPG400-L@xxxxxxxxxxxx<mailto:
RPG400-L@xxxxxxxxxxxx> To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx<mailto:
RPG400-L-request@xxxxxxxxxxxx>
Before posting, please take a moment to review the archives at
http://archive.midrange.com/rpg400-l.
--
This is the RPG programming on the IBM i / System i (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.