+ Start a Discussion
danlatdanlat 

Output day 'th', 'st', 'nd', 'rd' text

Hi All,

 

Does anyone know how to output the day text part? ie for 1st, it would be 'st', 2nd 'nd', etc. It doesnt seem possible with Java date formatting: http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html

 

Thanks in advance.

 

Message Edited by danlat on 03-18-2010 08:21 AM
Best Answer chosen by Admin (Salesforce Developers) 
kingkong94538kingkong94538

Create your own - it seems like simple if-else logic... but before you do that google it - you may find solution outside java world which should be quite easy to transform into java.

 

All Answers

kingkong94538kingkong94538

Create your own - it seems like simple if-else logic... but before you do that google it - you may find solution outside java world which should be quite easy to transform into java.

 

This was selected as the best answer
danlatdanlat

Hi,

 

Thanks for the reply.

 

I only mention Java because APEX uses the same date format patterns as Java. At the moment I have this:

 

 

<apex:outputText value="Dated: {0,date,d MMMMM yyyy}"> <apex:param value="{!NOW()}" /> </apex:outputText>

 

Which prints somthing like "8 July 2010", I was just wondering how to get "8th July 2010" ('th' should be superscript)

 

But your right, a simple IF would do.

 

Thanks

 

 

Alexis KasperaviciusAlexis Kasperavicius
Found this APEX code floating around from Soundar Rajan P. & Bhavin Mehta
 
<!--ST  01 && 21 && 31 -->
<apex:outputpanel rendered="{!IF(Day(Date__c) = 1 || Day(Date__c) = 21 || Day(Date__c) = 31 , TRUE, FALSE) }">
<apex:outputText value="{0,date,dd}<sup>st</sup> {0,date,MMMM} {0,date,YYYY}" escape="false"> 
<apex:param value="{!Date__c}" />
</apex:outputText>
</apex:outputpanel>

<!--ND 02 && 22 -->
<apex:outputpanel rendered="{!IF(Day(Date__c) = 2 || Day(Date__c) = 22  , TRUE, FALSE) }">
<apex:outputText value="{0,date,dd}<sup>nd</sup> {0,date,MMMM} {0,date,YYYY}" escape="false"> 
<apex:param value="{!Date__c}" />
</apex:outputText>
</apex:outputpanel>

<!-- RD 3 && 23 -->
<apex:outputpanel rendered="{!IF(Day(Date__c) = 3 || Day(Date__c) = 23  , TRUE, FALSE) }">
<apex:outputText value="{0,date,dd}<sup>rd</sup> {0,date,MMMM} {0,date,YYYY}" escape="false"> 
<apex:param value="{!Date__c}" />
</apex:outputText>
</apex:outputpanel>

<!-- TH 4 && 5- 20 && 24 -30 -->
<apex:outputpanel rendered="{!IF(Day(Date__c) != 1 && Day(Date__c) != 21 && Day(Date__c) != 21 && Day(Date__c) != 31 && Day(Date__c) != 2 && Day(Date__c) != 22 && Day(Date__c) != 3 && Day(Date__c) != 23, TRUE, FALSE) }">
<apex:outputText value="{0,date,dd}<sup>th</sup> {0,date,MMMM} {0,date,YYYY}" escape="false"> 
<apex:param value="{!Date__c}" />
</apex:outputText>
</apex:outputpanel>

 
Alexis KasperaviciusAlexis Kasperavicius
Using a formula on the object is another way:
 
IF(AND(MOD(DAY(Date__c),10) >= 11, MOD(DAY(Date__c),10) <= 13),"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"))))
Clifford CrittendenClifford Crittenden
When using the code posted on Oct 6th 2017, day 11 returns st, 12 returns nd, and 13 returns rd

There shouldn't be a MOD on the first line as it would turn 11 into 1, 12 into 2, etc.:
IF(AND(DAY(Date__c) >= 11, DAY(Date__c) <= 13),"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"))))