+ Start a Discussion
Chad KovacChad Kovac 

How can I output the current day with ND RD or TH in Visual Force?


Using this code, I can get the day of the month but I need to say: 2nd, not 2 and 3rd not 3, etc.,

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

Someone on Stack Exchange recommended this Javascript embedded in my page:
<Script Language="JavaScript">
public static String getDayOfMonthSuffix(Integer n) {
    if (n == null) {
        return '';
    }

    if (n >= 11 && n <= 13) {
        return 'th';
    }

    Integer modResult = Math.mod(n, 10);        
    if (modResult == 1) { 
        return 'st'; 
    } else if (modResult == 2) { 
        return 'nd'; 
    } else if (modResult == 3) { 
        return 'rd'; 
    } else { 
        return 'th';
    }
}</script>

html starting...
IN WITNESS WHEREOF, the Parties have executed this Confidentiality Agreement under seal as of this 
<Script Language="JavaScript">
    getDayOfMonthSuffix(getUTCDate());
</Script>
day of .... more html




James LoghryJames Loghry
I would do something similar, but in Apex.  Seems a bit overkill in Javascript, and at least with Apex you can unit test it easily (without setting up a JS testing framework).

Something like the following:
//Visualforce
On this {!todaysDate} blah blah

public class MyController{

    public String todaysDate {get; private set;}
    public String todaysMonth {get; private set;}

    public MyController(){
        Date today = Date.today();
        Integer todaysDayOfMonth = today.day();
        today
        //Reimplement day of month logic from Javascript here
        //todaysDate = 
        todaysMonth = today.month();
    }
}
If you get really ambitious, you could throw the text in a custom label for formatting / translatting as well.