Is this a good practice in displaying months?

Code Snippet:

<b>{!CASE( IF( (MONTH(TODAY()) +3) > 12,
               (MONTH(TODAY()) +3) - 12,
                MONTH(TODAY()) +3),
                 1, "January",
                 2, "February",
                 3, "March",
                 4, "April",
                 5, "May",
                 6, "June",
                 7, "July",
                 8, "August",
                 9, "September",
                10, "October",
                11, "November",

I'm using this in outputting the current month, previous month, next month of the current month, and so on. As you can see in the code snippet, the code is for the third month forward of the today's month. I realized this might not be a good approach. If this can be minified in different approach, it would be better, I would not be creating unnecessary line of codes.


You can use outputText and then format the date using the oracle SimpleDateFormat syntax

<apex:outputText value="{0,date,MMMM}">
        <apex:param value="{!TODAY()}" /> 

UPDATE based on your edited question

you can use the same approach to display the month in text for any date you want by changing the value of apex:param

for example, you can apply your first part of the formula in the param value like

   <apex:outputText value="{0,date,MMMM}">
        <apex:param value="{!DATE(YEAR(TODAY()),IF((MONTH(TODAY()) +3) > 12, (MONTH(TODAY()) +3) - 12,MONTH(TODAY()) +3),DAY(TODAY()))}" /> 


if you are using a custom / extension controller for your VF page, you can have a controller variable which can use addMonths method on Today() to give you the dynamic date you want and you can simply bind the apex:param value to the controller variable


public Date dynamicDate {
 get { return Today().addMonths(3); }

Visualforce Page

<apex:outputText value="{0,date,MMMM}">
        <apex:param value="{!dynamicDate}" /> 
| improve this answer | |
  • 1
    I haven't checked the links that you gave yet. But first, I want to ask, could this be used when you want to get the previous and current month of today's month or getting the month after next month? – rpm07 Sep 18 '14 at 4:59
  • 1
    Its not good to change the question after its answered (it makes the answer invalid). I have updated my answer based on your updated question. – Vamsi Krishna Sep 18 '14 at 6:25
  • Sorry for that, I did that to make the question more descriptive. I appreciate your effort in editing your answer to satisfy my question. – rpm07 Sep 18 '14 at 6:37
  • 1
    no worries.. hope now its helpful for you :-) – Vamsi Krishna Sep 18 '14 at 6:38
  • Yes! Your answer really helped me. :-) – rpm07 Sep 18 '14 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.