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
BenzyBenzy 

Help with VF page to select time zone from picklist and update date/time value on page

I've created a Visualforce page that lists a bunch of interview time slots with a link for people to reserve them. One of the columns is showing a date/time field. Since it is a public visualforce page, the date/time field defaults to GMT.

I would like to have a picklist containing all the time zones (worldwide), where the user selects their time zone from the list and then the date/time column updates to show the times based on the newly selected time zone.

I have gotten as far as creating the apex class and vf page which almost does the trick for the current system time (which is not very far!). I figure it's a good first step, but need some help as I'm stuck. I'm struggling to get the picklist value to pass into the timezone part of the apex class. Any ideas?

Thanks

Apex Class
public class ConvertTimeZone { 
    public String YourTimeZone {get;set;}
    public String tzOptions {get;set;}
    public String YourInterviewTime{get;set;}
    
    public List<SelectOption> getTimeZoneOptions() {
        List<SelectOption> tzOptions = new List<SelectOption>();
        tzOptions.add(new SelectOption('GMT','Greenwich Mean Time'));
        tzOptions.add(new SelectOption('Australia/Sydney','Australian Eastern Daylight Time'));
        tzOptions.add(new SelectOption('Asia/Kolkata','India Standard Time'));
 
        return tzOptions;
    }
    
    public ConvertTimeZone() {
    DateTime temp = System.now();
    YourInterviewTime = temp.format('dd/MM/yyyy HH:mm a z', 'tzOptions'); 
    }
}

VF Page
<apex:page controller="ConvertTimeZone">

<apex:form >
    <apex:pageBlock title="Custom PickList Demo" id="out">
        
        <apex:pageBlockSection title="Custom Picklist Using selectList and selectOption" collapsible="false">
            <apex:actionRegion >
            <apex:selectList value="{!YourTimeZone}" multiselect="false" size="1">
                <apex:selectOptions value="{!TimeZoneOptions}"/>
            <apex:actionSupport event="onchange" rerender="yourtimezone" />
            </apex:selectList>
            </apex:actionRegion>
        <apex:actionRegion >
            <apex:outputText id="yourtimezone" value="{!YourTimeZone}" label="You have selected:"/>
        </apex:actionRegion>
        </apex:pageBlockSection>

        <apex:pageBlockSection >
        Resulting Time: <apex:actionRegion ><apex:outputText id="yourtimezone" value="{!YourInterviewTime}"/></apex:actionRegion>
        </apex:pageBlockSection>
    </apex:pageblock>
  </apex:form>
</apex:page>

 
ManojjenaManojjena
Hi Benzy ,

Below code will help you display all time zone in picklist ,Please try to use .
public List<SelectOption> getTimeZone(){
  List<SelectOption> options = new List<SelectOption>();
  Schema.DescribeFieldResult fieldResult =User.TimeZoneSidKey.getDescribe();
  List<Schema.PicklistEntry> PkListEntry = fieldResult.getPicklistValues();
  for( Schema.PicklistEntry  pkEnt : PkListEntry) {
      options.add(new SelectOption(pkEnt.getLabel(), pkEnt.getValue()));
   }       
   return options;
}

<apex:selectList id="TimeZon" value="{!User.TimeZoneSidKey}"
         size="1" required="true">
  <apex:selectOptions value="{!TimeZone}"/>
</apex:selectList>
John Dressel 3John Dressel 3
Manoj, I have tried using your code as it looks like exactly what I want but I'm getting "Error: Unknown property 'ChangeTimeZone.User' and prompts me to either create an apex property ChangeTimeZone.User or create an apex method ChangeTimeZone.getUser. I took it almost verbatum from your post. What am I missing? (Sorry, I don't know how to post code formatted the way you did).


public Class ChangeTimeZone { 
     public List<SelectOption> getTimeZone(){
      List<SelectOption> options = new List<SelectOption>();
      Schema.DescribeFieldResult fieldResult = User.TimeZoneSidKey.getDescribe();
      List<Schema.PicklistEntry> PkListEntry = fieldResult.getPicklistValues();
      for( Schema.PicklistEntry  pkEnt : PkListEntry) {
          options.add(new SelectOption(pkEnt.getLabel(), pkEnt.getValue()));
       }       
      return options;
    }      
}


<apex:page controller="ChangeTimeZone">
  <apex:form >
      <apex:pageBlock title="Change Time Zone" >
         <apex:pageBlockSection columns="1"  >
             <apex:pageBlockSectionItem >
                <apex:selectList id="TimeZon" value="{!User.TimeZoneSidKey}" size="1" required="true">
                      <apex:selectOptions value="{!TimeZone}"/>                                        
                </apex:selectList>  
             </apex:pageBlockSectionItem>
         </apex:pageBlockSection> 
      </apex:pageBlock>
  </apex:form>
</apex:page>
Eric KintzerEric Kintzer
Change the code posted by Manojj to:
 
public User proxyU {  // holds user selected-timezone defaults to running user's timezone
    get {
      if (proxyU == null)
         proxyU = [select id, timezonesidKey from User where id = :UserInfo.getUserID()];
     return proxyU;
   }
   set;
}

public List<SelectOption> getTimeZones(){
  
  List<SelectOption> options = new List<SelectOption>();
  Schema.DescribeFieldResult fieldResult =User.TimeZoneSidKey.getDescribe();
  List<Schema.PicklistEntry> PkListEntry = fieldResult.getPicklistValues();
  for( Schema.PicklistEntry  pkEnt : PkListEntry) {
      options.add(new SelectOption(pkEnt.getLabel(), pkEnt.getValue()));
   }       
   return options;
}

<apex:selectList id="TimeZon" value="{proxyU.TimeZoneSidKey}"
         size="1" required="true">
  <apex:selectOptions value="{!TimeZones}"/>
</apex:selectList>

 
RustyboyRustyboy
I found this post really useful, especially the last from Eric, though I did spend a couple of hours debugging the small logic error

Syntax error
There is a missing exclamation mark "!" ahead of the fieldname in this statement - it should be: "{!proxyU etc..."
<apex:selectList id="TimeZon" value="{proxyU.TimeZoneSidKey}"

Logic error
The order of the label and value are back to front in the pickList options.add statement. Pick lists should have value then label. The correct code should be:
options.add(new SelectOption(pkEnt.getValue(), pkEnt.getLabel()));

Thanks all the same. It works a treat!