0

I am trying to format the datetime field(LastCUUpdateDate) returned from query. I tried the solution given by @Suri in Question#73600 here but it turns out to be GMT always. So, posting the question with the piece of code and what I am trying to achieve.

Page:

<apex:page standardcontroller="account" extensions="conts">
{!account.name}
<apex:pageBlock >
<apex:pageBlockTable value="{!contacts}" var="c">
    <apex:column value="{!c.name}"/>
    <apex:column value="{!c.id}"/>
    <apex:column value="{!c.LastCUUpdateDate }"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>

extension class:

public class conts {
public conts(ApexPages.StandardController controller) {    }
public string currentRecordId;
public list<contact> getcontacts(){
    currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
    list<contact> c = [SELECT id, name, LastCUUpdateDate FROM contact WHERE accountId = :currentRecordId];
    return c;
}
}

What I am trying to do ? - return datetime field formatted something like Apr 21' 2015 06:53PM

1

1 Answer 1

1

Instead of using the straight column:

<apex:column value="{!c.LastCUUpdateDate }"/>

Use the format version instead:

<apex:column>
<apex:outputText value="{!c.LastCUUpdateDate}" />
</apex:column>

and modify your controller to do the timezone conversion for you:

public class conts {
public conts(ApexPages.StandardController controller) {    }
public string currentRecordId;
public list<con> getcontacts(){
currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
list<contact> c = [SELECT id, name, LastCUUpdateDate FROM contact WHERE accountId = :currentRecordId];
list<con> clist = new list<con>();
for(contact x:c){
clist.add(new con(x));
}
return clist;
}

public class con{
  public string name {get; set;}
  public string cd {get; set;}
  public ID id {get; set;}
  public con(Contact c){
    this.name=c.name;
    this.id=c.id;
    this.cd = c.LastCUUpdateDate.format('MMM dd yyyy HH:mm a');
  }
}
}
8
  • Hi Jim, I tried to do that which is also mentioned in solution for question#73600 but the return value is always in GMT. Please let me know if I am doing anything wrong there. Thanks
    – sf_user
    Commented Apr 28, 2015 at 16:07
  • 1
    What is the format you would like to dispaly then, you are mentioning GMT can you elobarate more about it. As per my under standing you were in different time and it is displaying another time zone. can you share the format.
    – Suresh
    Commented Apr 28, 2015 at 16:18
  • Sorry, didn't notice the GMT issue. That is a long standing gap. There are 2 options. One, if you can use the standard format, if you just use outputtext and put a space before the name <apex:outputtext value="{ !c.dateval}" /> then it will convert to local timezone. Or, if you need a specific format, use a wrapper class on your output to reformat the date in the apex class, see up updated answer above.
    – JimRae
    Commented Apr 28, 2015 at 17:46
  • @Suri. I am in GMT + 8 timezone and i created record as per this time but the format mentioned gives me time that is 8 hour back
    – sf_user
    Commented Apr 28, 2015 at 17:52
  • I want to post date and time in 2 separate fields. So want to split them something like using paras as suggested earlier where I can pass the datetime twice once I pull the date and then I get the time but should be current time instead of GMT. Thanks
    – sf_user
    Commented Apr 28, 2015 at 17:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .