I probably saw this question 100 times, but still don't understand. So basically I want to display only the Time from a datetime field but not the Date. However I always get the GMT time. I know I could create a wrapper and format my datetimes in the controller. But why should I create a wrapper class only to display datetimes. I made some tests and here is what I got :

//not good, display GMT time
<apex:outputText value="{0,date,HH:mm}">
     <apex:param value="{!item.Inspection_Date_Time__c}" name="p1" />
</apex:outputText>

//not good, display GMT time
<apex:outputText value="{0,number,00}:{1,number,00}">
    <apex:param value="{!item.Inspection_Date_Time__c.time.hours}" name="p1" />
    <apex:param value="{!item.Inspection_Date_Time__c.time.minutes}" name="p2" />
</apex:outputText>

//time is correct but I want only the time not the date
<apex:outputText value="* {!item.Inspection_Date_Time__c}" />

//display GMT time, and what's the difference with the previous line ?
<apex:outputText value="{!item.Inspection_Date_Time__c}" />

//time is correct but I want only the time not the date
<apex:outputField value="{!item.Inspection_Date_Time__c}" />

Anyone found a solution for that(except via the controller)?

Cheers,

David

Use Apex to get User's TimeZone and offset time. e.g.

<apex:page controller="TimeZoneController">
    <apex:outputText value="{0,date,dd/MM/yyyy HH:mm:ss}">
        <apex:param value="{!NOW()+offset}"/>
    </apex:outputText>
</apex:page>

public class TimeZoneController{
    public Double offset{get{
        TimeZone tz = UserInfo.getTimeZone();
        //Milliseconds to Day
        return tz.getOffset(DateTime.now()) / (1000 * 3600 * 24.0);
    }}
    public TimeZoneController(){
    }
}

[Reference] https://www.xgeek.net/salesforce/display-datetime-with-timezone-in-visualforce-page/

  • Wow, your solution is awesome! – manza Nov 24 '15 at 6:56
  • I do not think this accounts for Daylight Savings Time. The offset would need to be calculated on an item by item basis. E.g. Created Date could be in Daylight Time, and Last Modifed could be in Standard TIme. Hence using the same offset would be incorrect. But only out by an hour for some scenarios. – NZ Dev Oct 10 '17 at 10:28

I faced same issue again and again. It shows the GMT time. I created a Map of Map and then Show it on visualforce page. It is working fine

Map<item,String> timeMap = new Map<Item,String>();
for(Item i : itemList){
String t = String.valueof(i.TIMEFIELD)
t=t.substring(0,5);
timeMap.put(r.id,t);
}

On Visualforce page you can show it like:

{!timeMap[ro.id]}
  • Creating a Map is much the same as creating a wrapper. So it is implied that a map is not wanted (by OP) Solution not via controller. – NZ Dev Oct 10 '17 at 10:30

Update

You can try this crazy solution. Just use javascript to get the time:

<script>
    jQuery(document).ready(function(){
        jQuery('.timeValue').children().each(function(){
            var currentText = jQuery(this).text();
            jQuery(this).text( currentText.substring(currentText.lastIndexOf(' '), currentText.length) );
        });
    });   
</script>

<apex:outputPanel styleClass="timeValue">
    <apex:outputField value="{!testacc.createdDate}"  />
</apex:outputPanel>

Well, the apex and Datetime object are your friends!

hour: Returns the hour component of a Datetime in the local time zone of the context user
minute: Returns the minute component of a Datetime in the local time zone of the context user

(From here: Datetime Methods)

So why can't you just create a time you want from a goven Datetime?

Controller: public String timeOk { get; set; } public Account testacc { get; set; }

testacc = [select createdDate from account limit 1];
timeOk = testacc.createdDate.hour() + ':' + testacc.createdDate.minute();

Page:

<apex:outputText value="{!timeOk}" />
  • It's for a list of records, and I know how to do it via a controller. I just don't want to create a wrapper class only to display a list of records, and why salesforce doesn't support a thing so simple ?! But thanks – Cloud Ninja Aug 27 '13 at 8:29
  • Try javascript? ;) Updated my answer. – Sergej Utko Aug 27 '13 at 9:05
  • Haha yeah with Javascript of course, I prefer the wrapper class then lol ;-). But isn't it crazy there is nothing for Visualforce ?! – Cloud Ninja Aug 27 '13 at 9:15
  • Yeah, Visualforce is not yet perfect :() – Sergej Utko Aug 27 '13 at 9:19

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.