Monday, September 5, 2011

Changing Date Format On Visualforce Page in Salesforce Apex

Dear Guys....

We have a some idea regarding date format on visualforce page.Usually when we display the date field value on visualforce page it make the date format.
In case we dont want to see this format we need to show only the format which should be easy to understand to all like some different formats.
MM-DD-YYYY or YYYY-MM-DD Like these format.

We have to ways to achieve the date.
1. We can create a method by passing parameter type Date or Date Time whatever you passing depend on you.
And then call this method before showing the date field value of visualforce page.

Opportunity opp =  [select CloseDate from Opportunity Limit 1];
string DateString1 = ConvertDateFormat(opp.CloseDate);
public string ConvertDateFormat(Date d){ 
string DateString ;
string month;
  if(d!=null){
     integer d1 = d.day();
     integer d2 = d.month();
     if(d2 == 1)
      {
        month = 'january';     
    }
    if(
        d2 == 2){month = 'February'; 
     }
    if(
        d2 == 3){month = 'March';   //so on.............................
     }
    integer d3 = d.year();
    DateString = string.valueOf(d1)+'-'+month+'-'+string.valueOf(d3);
}return DateString;

}

system.debug('***********'+DateString1);
So guys in this why we can format our date as we need.
2. Apart from this code we have another way to directly format the date on visualforce page.
We have a Apex Tag <apex:parma> which pass the value to the output text..
Lets say we have a date Field we can directly pass it to the visualforce tag.

<apex:outputText value="{0,date,dd-MMMM-yyyy}">
                  <apex:param value="{! Opp.CloseDate}" />
</apex:outputText>

Highlighted text decided the in what way your date format would be display on visualforce page. You can this format by replacing the value in the format.
{0,date,dd-MMMM-yyyy}
{0,date,MMMM-dd-yyyy}
{0,date,MMMM-yyyy-dd}

{0,date,dd-MM-yy}
{0,date,MM-dd-yyyy}
{0,date,MM-yy-dd}

You can check more probabilities by your self will make better understanding for you.

Please promote it this post help you and also let me know if I am doing something wrong in the post.

Thanks

Cheers.........................



5 comments:

  1. In your first example, if you convert your Date object to a DateTime, you can use the Java based 'format' method, which is much easier!

    DateTime dt = DateTime.newInstance(d.year(), d.month(), d.day());
    return dt.format('yyyy-MM-dd'); // Use any formatting you want!

    ReplyDelete
    Replies
    1. Good One...it helps me a lot :-)

      Delete
  2. How do I make the date come out in text format: I.e. January 10, 2012?

    ReplyDelete
  3. Hi Nick,
    I Just use the custom method having parameter either Date Or DateTime which returns a string value as I had mentioned in this post.
    I do not know if directly we can do it .


    Just manipulate string values :
    DateString = month+string.valueOf(d1)+','+string.valueOf(d3);

    ReplyDelete
  4. good post with proper example
    thanks

    ReplyDelete