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 2

Status
Not open for further replies.

jrowden773

Programmer
Oct 19, 2007
9
0
0
Hello I am modifying a program that haves two integer fields one called (screen field)@CDATE and the other (field from file)RCDATE that are 6 character fields. Now my problem is is that @CDATE is *MDY format while RCDATE is in *YMD format and I'm trying to get the RCDATE format to *MDY also so when I do a :

EVAL @CDATE = RCDATE


They both have the same format.
 
Use the %Date() Built-in Function.
%DATE{(expression{:date-format})}
%DATE converts the value of the expression from character, numeric, or timestamp data to type date. The converted value remains unchanged, but is returned as a date. The first parameter is the value to be converted. If you do not specify a value, %DATE returns the current system date. The second parameter is the date format for character or numeric input. Regardless of the input format, the output is returned in *ISO format.

Anyway you'd be better off defining a date field on the screen record format.

Example

Code:
d RcDate          s              6p 0 inz(071115) 
d ScreenDate      s               d   datfmt(*MDY)

C         EVAL    ScreenDate = %Date(RcDate : *YMD)

If RCDATE is '071115', ScreenDate now contains d’2007-11-15’
 
I may get some hate mail but that's OK. If you want to kick it old school, there is a little trick from back before Bill gates was just a little tadpole.

Multiply cdate by 10000.01. It will arithmetically convert mdy to ymd. To convert back use 100.0001. This is supposed to be inefficient and is not recommended. It used to be one of the ways we converted dates. The other is to use two data structures that break the fields into the month, day, and year parts. Then move the cdate parts to the rcdate parts and vice versa.

Today, I would do what Mercury2 suggested. But, there is still a lot of old code around using one of the two other methods, or both.

A little trip down memory lane.
 
>>Regardless of the input format, the output is returned in *ISO format. <<

Not true. The date format is whatever the result field is defined. In this example, ScreenDate is defined as *MDY so will be 111507.

For the orig request:
EVAL @CDATE = %dec(%date(RCDATE:*MDY):*YMD)

Note that this will fail if RCDATE is not a valid date.
 
>>Regardless of the input format, the output is returned in *ISO format by default. <<
Although I copied this text from the ILE RPG Reference Guide, my previous posting should have read by default at the end of the sentence.
Now if both dates are 6 char fields, here's what i'd do
Code:
d RcDate          s              6a   inz('071115')    
d CDate           s              6a                    

C         EVAL    CDate = %Char(%Date(RcDate : *YMD0) : *MDY0)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top