0

page

 <apex:variable value="{!activity}" var="act">
    <apex:outputText value="{0,date,h:mm a}">
    <apex:param value="{!act.Date__c}" /> 
    </apex:outputText>

controller..

        public list <Activity__c > activity {get;set;}


        public GenerateInvoiceController(){

activity= [SELECT Id, Name, Date__c, Quantity__c, 
                              Activity_Lookup_Name__c, Day__c,
                              Unit_Price__c ,
                              Guest_Notes__c, Inhouse_Notes__c
                               FROM Activity__c 
                               WHERE Reservation__c =:  reservationId];
}

Right now I have this code for displaying time. One problem is that it has to show pacific time all the time.(not user's current timezone.)

Any suggestions?

Thank you in advance

  • In your controller you'll need to use dateGMT() and timeGMT() to get the GMT components of date and time into one or two properties (Either Date and Time or String properties), then display the properties instead of the field itself. – David Cheng Sep 29 '18 at 5:10
1

You can set the timezone in your controller class, like this:

Assuming myDateTime is a DateTime in GMT, this will give you the time in Pacific Time:

myDateTime.format('h:mm a', 'America/Los_Angeles');

In this case, you can use a wrapper class to add the formatted DateTime to the items in the list you're displaying in your Visualforce page. Try this:

public list <ActivityWrapper> activityWrapper {get;set;}

public GenerateInvoiceController(){

    activity= [SELECT Id, 
                      Name, 
                      Date__c, 
                      Quantity__c, 
                      Activity_Lookup_Name__c, 
                      Day__c,
                      Unit_Price__c ,
                      Guest_Notes__c, 
                      Inhouse_Notes__c
                 FROM Activity__c 
                WHERE Reservation__c = :reservationId];
}

public ActivityWrapper {
    Activity__c activity {get; set;}
    DateTime pacificTime {get; set;}

    // constructor
    activityWrapper(Activity__c act){
        activity = act;
        pacificTime = formatDate(act.Date__c);
    }

    private formatDate(DateTime dt){
        return dt.format('h:mm a', 'America/Los_Angeles');

    }
}

In your Visualforce markup, you can access each activity's properties like this:

{!activityWrapper.activity.Quantity__c}

...and your formatted DateTime like this:

{!activityWrapper.pacificTime}
  • I updated the question... – Logan H Sep 29 '18 at 11:52
  • @LoganH -- Got'cha. So it's a list of Activity__c. I updated the answer. – Shane Steinfeld Sep 29 '18 at 14:27
-1
<apex:page controller="TimeZoneController">
<apex:outputText value="{0,date,dd/MM/yyyy HH:mm:ss}">
    <apex:param value="{!NOW()+offset}"/>
</apex:outputText>

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(){
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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