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

This gets me the day # of the month but I need the postfix portion of the date ie: 2nd, 3rd, 4th.

10

You can utilize a method like this in your controller to append the suffix to the day portion of the date, but this approach won't work directly in VF using an <apex:param /> input into a formatter. You'll have to construct the date string in the controller.

Caveat: this code doesn't work as written for integer values greater than 100 - but there are few months with more than 100 days, so I figured it was safe enough as written.

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';
    }
}

Update

Here's some example controller code and VF markup

VF

<apex:outputText value="{!showFormattedDate}" />

Apex

public with sharing YourController {

    public string showFormattedDate() {

        // get a date
        Date today = Date.today();

        // use some parameter substitution to build the string
        string formattedDate = String.format('{0} {1}, {2}', new List<String>{ today.month(), getDayOfMonthSuffix(today.day()), today.year() });

        return formattedDate;
    }

    public 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';
        }
    }
}
  • Hmm seems good, how do I get the result? <script ....> getDayOfMonthSuffix(????); </script> – General Chad Jun 27 '14 at 20:32
  • @GeneralChad Take a peek at the answer, I added a working example that should get you started. – Mark Pond Jun 27 '14 at 21:25
  • Thanks Mark! -- Now to get a date formatted into Quarters. ;) I think using FLOOR((MONTH(CloseDate)-1)/3) + 1 and a modified script from what you posted above might work. – General Chad Jul 2 '14 at 21:14
3

You can also add it as a formula on the object if you don't want to make/use a custom controller:

IF(ISBLANK(Date__c),NULL, 
IF(AND(DAY(Date__c) > 10, DAY(Date__c) < 14),"th", 
IF(MOD(DAY(Date__c),10) = 1,"st", 
IF(MOD(DAY(Date__c),10) = 2,"nd", 
IF(MOD(DAY(Date__c),10) = 3,"rd","th")))))

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.