Convert RPG Datefield %DATE into a signed numeric 

 July 11, 2011

By  NickLitten

To convert a RPG DATEFIELD to this numeric 8,0 field with no ‘/’ or ‘-‘ do this:

RPG Code Snippet
D USADate 8s 0
USADate = %dec(%char(DateField:*iso0):8:0);

Or another even neater function is this:

USADate = %uns(%char(DateField:*USA0));

I prefer this %UNS built in function, since it does not require me to specify length and decimal position parameters.

What is %UNS?

%UNSH (Convert to Unsigned Format with Half Adjust)

%UNSH(numeric expression)

%UNSH is like %UNS except that if the numeric expression is a decimal or a float value, half adjust is applied to the value of the numeric expression when converting to unsigned type. No message is issued if half adjust cannot be performed.

*..1....+....2....+....3....+....4....+....5....+....6....+....7...+.... D*Name++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++
D p7 s 7p 3 inz (8236.567)
D s9 s 9s 5 inz (23.73442)
D f8 s 8f inz (173.789)
D result1  s  15p 5
D result2  s  15p 5
D result3  s  15p 5
D array s 1a dim (200)
D a s 1a
 result1 = %uns (p7) + 0.1234; // "result1" is now 8236.12340
 result2 = %uns (s9); // "result2" is now 23.00000
 result3 = %unsh (f8); // "result3" is now 174.00000
 // %UNS and %UNSH can be used as array indexes
 a = array (%unsh (f8));



IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"cff50":{"name":"Main Accent","parent":-1},"a344d":{"name":"Accent Transparent","parent":"cff50"}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"cff50":{"val":"var(--tcb-skin-color-0)"},"a344d":{"val":"rgba(46, 138, 229, 0.85)","hsl_parent_dependency":{"h":210,"l":0.54,"s":0.78}}},"gradients":[]},"original":{"colors":{"cff50":{"val":"rgb(0, 178, 255)","hsl":{"h":198,"s":1,"l":0.5}},"a344d":{"val":"rgba(0, 178, 255, 0.85)","hsl_parent_dependency":{"h":198,"s":1,"l":0.5}}},"gradients":[]}}]}__CONFIG_colors_palette__

Get In Touch

I’m always looking for awesome input, feedback and critique!