I have an (largely) formatting issue that I would like to solve in a neat way if possible.
When using a natural date format, I sometimes want to display it natively as a ‘nice’ date.
I can use the edit-mask properties to do some manipulation on the date format to get it to specific string representations. That is great.
Recently I was asked to present the date in the format ‘Weekday Month DD, YYYY’.
I can do this with:
define data local
01 #date-alpha (a8)
01 #date (d)
01 #date-print (a30)
end-define
#date-alpha := *datn
move edited #date-alpha to #date(em=yyyymmdd)
move edited #date(EM=N(9)' 'L(10)^DD','^YYYY) to #date-print
print #date-print
end
However, when the date is early in the month, the ^DD result appears as ‘08’ which is not pretty.
So I’ve replaced it with
move edited #date(EM=N(9)' 'L(10)^ZD','^YYYY) to #date-print
which now results in the 0 being replaced with a blank character.
Now I’m left with the task of eliminating this character from the string.
examine #date-print for ' ' replace ' '
was my first attempt, however, the double space is simply recognized as a blank string array. Similarly, H’2020’ also resulted in no observable change.
I could create a more complex set of statements to solve this for this specific case:
define data local
01 #date-alpha (a8)
01 #date (d)
01 #date-print (a30)
01 #pos (i4)
01 #len (i4)
01 #i (i4)
01 #is-space (L)
end-define
#date-alpha := *datn
move edited #date-alpha to #date(em=yyyymmdd)
move edited #date(EM=N(9)' 'L(10)^ZD','^YYYY) to #date-print
examine #date-print for ' ' giving length #len
for #i = 1 to #len
#pos := #len - #i + 1
examine substring(#date-print, #i, #pos) for ' ' giving position #pos
if #pos = 1
if #is-space = true
#pos := #len - #i + 1
examine substring(#date-print, #i, #pos) for ' ' delete
examine #date-print for ',2' replace ', 2' /* fix for the year space as it gets caught in the remaining string
else
#is-space := true
end-if
else
#is-space := false
end-if
end-for
print #date-print
end
I feel like there should be a universal, simple method to eliminate a double space in a string within Natural. Would anyone care to elaborate? Maybe a simpler version of the per-character for loop which spits each element into a new string only if the previous one wasn’t a space?
Thanks!