function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DcoderDcoder 

Showing date time with Apex:outputtext

Hi All,

 

 I am trying to show date time with Apex:outputtext using the below code:

 

<apex:outputText value=" {0,date,MM/dd/yy HH:mm a}">
       <apex:param value="<DateTime field>"/></apex:outputText>

The result of this is date is coming in GMT timezone. Please let me know how can we show it in user's timezone.

 

thanks!

Best Answer chosen by Admin (Salesforce Developers) 
SSRS2SSRS2

 

<apex:page controller="sampleCon">
 <apex:outputText value="{!dateTimeValue}"/>
</apex:page>

public class sampleCon {
    public String dateTimeValue { get; set; }
    public sampleCon() {
      dateTimeValue = System.Now().format('MM/dd/yy HH:mm a', 'PST');//GMT
    }
}

- Suresh

 

All Answers

SSRS2SSRS2

 

<apex:page controller="sampleCon">
 <apex:outputText value="{!dateTimeValue}"/>
</apex:page>

public class sampleCon {
    public String dateTimeValue { get; set; }
    public sampleCon() {
      dateTimeValue = System.Now().format('MM/dd/yy HH:mm a', 'PST');//GMT
    }
}

- Suresh

 

This was selected as the best answer
DcoderDcoder

Thanks Suresh!

 

yes, that is a workaround for this issue.

 

But, I was thinking if we have any way to show the time in particular timezone like CST or PST.

 

Does anyone else has any idea about it?

 

 

thanks!

DcoderDcoder

 

I meant, Do we have any way to show the time in CST timezone using Outputtext ?

aballardaballard

OuputField displays the time converted to the current users timezone.  

 

OutputText just displays whatever is stored in the datetime object, which is normally a GMT based time.  So you would need to use some apex code as in the example above to get a datetime value in the timezone you want. 

DcoderDcoder

okay, thanks !

 

I am doing it through apex code now.

 

 

sfuser2010sfuser2010

Were you able to to write that code Dcoder?  I'm having the same issue and would like the outputText to show the time in PST.  Please share your findings!

DcoderDcoder

@sfuser2010

 

I have written an extension for that VF page, in short I am storing the date  in string variable in that extension and showing that string variable on the VF page.

 

public class ext_sample {
	
	public String v_time{get;set;}

	public <custom object> pr{get;set;}
	
	public ext_sample(ApexPages.StandardController controller) {
		
		<custom object> prc = (<custom object>) controller.getRecord();
		
		pr = [select Id,CreatedDate from <custom object> where Id=:prc.Id];
		
		v_time = pr.CreatedDate.format('MM/dd/yyyy hh:mm a');
	}
	
	static testmethod void test1() {
		PageReference v_pg;
		
		<custom object> pr = [select Id from <custom object> limit 1];
		
		ext_sample prt = new ext_sample(new ApexPages.StandardController(pr));
	}

}