Variable for Current Month

LawnyToast

New Member
Hello ProgressTalk! This is my first post so I will be brief. I am trying to find the most efficient way to display the current month in string form. Right now I have.

iMonth = month(today)

cCurrentMonth = if iMonth = 1 then 'January' else if iMonth = 2 then 'February' else if iMonth = 3 then 'March' else if iMonth = 4 then 'April' else if iMonth = 5 then 'May' else if iMonth = 6 then 'June' else if iMonth = 7 then 'July' else if iMonth = 8 then 'August' else if iMonth = 9 then 'September' else if iMonth = 10 then 'October' else if iMonth = 11 then 'November' else if iMonth = 12 then 'December'.

The variables are all defined towards the top of my procedure. I believed this would be a relatively simple thing to accomplish. I think I might have forgotten something basic here.
 

Rob Fitzpatrick

ProgressTalk.com Sponsor
Welcome Lawny! Please enclose code samples within [ CODE ] and [ /CODE ] tags (remove the spaces) so it is easier to read.

For something like this it would be better to use a CASE statement rather than many ELSE IFs.

E.g.:
Code:
case iMonth:
 
  when  1 then cCurrentMonth = 'January'.
  when  2 then cCurrentMonth = 'February'.
  when  3 then cCurrentMonth = 'March'.
  when  4 then cCurrentMonth = 'April'.
  when  5 then cCurrentMonth = 'May'.
  when  6 then cCurrentMonth = 'June'.
  when  7 then cCurrentMonth = 'July'.
  when  8 then cCurrentMonth = 'August'.
  when  9 then cCurrentMonth = 'September'.
  when 10 then cCurrentMonth = 'October'.
  when 11 then cCurrentMonth = 'November'.
  when 12 then cCurrentMonth = 'December'.
 
  otherwise do:
    /* uh oh... */
  end.
 
end case.
 

TomBascom

Curmudgeon
Code:
define variable monthList as character no-undo
  initial "January,February,March,April,May,June,July,August,September,October,November,December"
.

message entry( month( today ), monthList ).
 

TomBascom

Curmudgeon
One more thing: "hungarian notation" (the practice of pre-pending gibberish in front of perfectly good identifer names) is an abomination and an affront to readability. Even Microsoft has seen the light and deprecated the practice.
 

LawnyToast

New Member
Fantastic! You guys are alright! I didn't think about 'case' in this situation. I am learning, and I will take all the advice I can get. You will definitely see me again.
 

Osborne

Active Member
Another option, although Windows and later versions of Progress only, but for reference:
Code:
MESSAGE CAST(BOX(TODAY),System.DateTime):ToString("MMM") SKIP
        CAST(BOX(TODAY),System.DateTime):ToString("MMMM")
        VIEW-AS ALERT-BOX.
 

Stefan

Well-Known Member
Since there are so many words that are reserved keywords, unlike Tom, I still like Hungarian notation.

An array can also be used in this case, the only downside of an array is if you want to go the other way (from name to number) that you cannot use lookup:

Code:
define variable cmonth as character no-undo extent 12 initial [ 
   "January", 
   "February", 
   "March", 
   "April",
   "May",
   "June",
   "July",
   "August",
   "September",
   "October",
   "November",
   "December"
].

message cmonth [ month( today ) ] view-as alert-box.
 

TomBascom

Curmudgeon
I prefer readable code. Slapping gibberish onto a perfectly good name does not improve readability.

In any event, as Stefan points out, one of the really nice things about the delimited list approach is that it is easily reversible. Use ENTRY() to get the month name or LOOKUP() to get the month number.
 
Top