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
Abhishek Singh 116Abhishek Singh 116 

<apex:inputText> is fetching date in different format while my visualforce site getting loaded.

I am using visualforce site, there I have a inputtext button which is binded to contact standard button "Birthdate". The date formate in Birthdate field field is MM/DD/YYYY. But when my visualforce site getting loaded it shows the format like "Fri May 20 00:00:00 GMT 2016". I want to show the date in MM/DD/YYYY format only. Please help.

Visualforce code is below:
<apex:inputText id="brhdate" size="10" value="{!contactEdit.Birthdate}"  html-readonly="true" onmouseover="initialiseCalendar(this, '{!$Component.brhdate}')" />

This is a inputText field so we can also enter value using calender. But it also should appear the value if it exist in that contact already.
My problem is while site is loaded, this field shows existing date in different format like "Fri May 20 00:00:00 GMT 2016". Please tell me how can I show the date in MM/DD/YYYY.

Thanks you in advance.
 
Best Answer chosen by Abhishek Singh 116
sandhya reddy 10sandhya reddy 10
Abhishek,

If  you are using controller u can change the format in your controller
see below code for reference

myDate (this is ur date feild)
String dayString = myDate.format();
and pass the string in vf page inputtext

Thanks
sandhya

All Answers

sandhya reddy 10sandhya reddy 10
Hi Abhishek,

Try to use inputfield instead of inputtext.

Thanks
sandhya Reddy
Abhishek Singh 116Abhishek Singh 116
Hi Sandhya,

Initially I used inputField like below:
<apex:inputField value="{!contactEdit.Birthdate}" type="date" required="true" />

But I date was coming in DD-MM-YYYY format. I need the formate to be in MM/DD/YYYY. 

Thanks!!
sandhya reddy 10sandhya reddy 10
Abhishek,
Go to

yourname -> setup -> my personal information

change your locale to english(united states) to get the display as mm-dd-yyyy.

Thanks
sandhya


 
surya7628@gmail.com Ksurya7628@gmail.com K
Hi Abhishek,

insted of using the inputtext use inputfield, i think we will get  date format like this MM/DD/YYYY.

Thanks,
Surya
sandhya reddy 10sandhya reddy 10
Abhishek,

Below is the explaination for date formats


Date format is controlled by the individual users Locale settings. 

What format is used for dates, times, and names in Salesforce?

The format used for dates, times, and names of people in Salesforce is determined by your Locale setting.
The Locale setting affects the format of date, date/time, and number fields, and the calendar. For example, dates in the English (United States) locale display as 06/30/2000 and as 30/06/2000 in the English (United Kingdom) locale. Times in the English (United States) locale display using a twelve-hour clock with AM and PM (for example, 2:00 PM), whereas in the English (United Kingdom) locale, they display using a twenty-four-hour clock (for example, 14:00).
The Locale setting also affects the first and last name order on Name fields for users, leads, and contacts. For example, Bob Johnson in the English (United States) locale displays as Bob Johnson, whereas the Chinese (China) locale displays the name as Johnson Bob.
For Personal Edition users, the locale is set at the organization level via Your Name | Setup | Company Profile | Company Information. For all other users, their personal locale, available at Your Name | Setup | My Personal Information | Personal Information, overrides the organization setting.
To find out what date/time format your Locale setting uses:
Click Your Name | Setup | My Personal Information | Personal Information.
View the date/time format used in the read-only Created By field. This is the format you should use for entering dates and times in Salesforce fields.
 
Abhishek Singh 116Abhishek Singh 116
Hi Sandhya,

Thanks for your help. But it hasn't fixed yet..

My Locale setting is already set as English(United States). In contact detail page I can see all the date in MM/DD/YYYY formate but in visualforce site the date is coming in different format.

Thanks!!
ManojjenaManojjena
Hi Abhishek,

try to implement like below it will help !!
<apex:outputText value="{0,date,MM/dd/yy}"> 
  <apex:param value="{!Datefield Name}" /> 
</apex:outputText>

Let us know if it helps !!
Thanks 
Manoj
Abhishek Singh 116Abhishek Singh 116
Hi Manoj,

It is working but I need to insert the date too. I can't use <apex:outputText>. I am using the <apex:inputText> so that I can enter the date using calender and it also shows the value which already exist in that record. 

Thanks!!
ManojjenaManojjena
Hi Abhishek,
if you want input field then automatically it willcome as mm/DD/YYYY  format .
Can you post  a screen shot if possible in  which format you are getting .
Thanks 
Manoj
Abhishek Singh 116Abhishek Singh 116

User-added image

In my VisualForce site, I am getting date in this format.
ManojjenaManojjena
Hi Abhishek,

try to add language attribute in page like below .
<apex:page standardController="Opportunity"  language="en_Us">

Let me know if it helps !!
Thanks 
Manoj
sandhya reddy 10sandhya reddy 10
Abhishek,

If  you are using controller u can change the format in your controller
see below code for reference

myDate (this is ur date feild)
String dayString = myDate.format();
and pass the string in vf page inputtext

Thanks
sandhya
This was selected as the best answer
Abhishek Singh 116Abhishek Singh 116
Hi Sandhya,

It is working now. Thanks for your help.
RAVITEJA C 19RAVITEJA C 19
Hi
I have facing one issue i want "select" option for the dropdown list of objects so can you pls and verify issue
Error:
Constructor not defined: [System.SelectOption].<Constructor>(String)

Apex class:

public class SchemaController{

    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

    public String selectedObject {get; set;}

    public String selectedField {get; set;}

   

    Public SchemaController()
    {   
        selectedObject = 'Account';

       
    }

    public List<SelectOption> getObjectNames() 
    {


        List<SelectOption> objNames = new List<SelectOption>();
         
       

        List<String> entities = new List<String>(schemaMap.keySet());

        entities.sort();

        for(String name : entities)
        {
            objNames.add(new SelectOption('--Select--'));
            objNames.add(new SelectOption(Name,Name));
        }
        return objNames;

     }

     public List<SelectOption> getObjectFields() {

            Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

            Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);

            Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();

            List<SelectOption> fieldNames = new List<SelectOption>();

            for (String fieldName: fieldMap.keySet()) 
            {  
                fieldNames.add(new SelectOption(fieldName,fieldName));
              
            }
            return fieldNames;
      }       
}

visualforce page:

<apex:page controller="SchemaController">
<apex:form> 
      <apex:pageBlock>
          <apex:pageBlockSection title="Objects & Fields" columns="2">

              <apex:pageBlockSectionItem >
                  <apex:outputlabel value="Object Names :"/> 
                      <apex:actionRegion >      
                           <apex:selectList value="{!selectedObject}" size="1">
                                    <apex:selectOptions value="{!ObjectNames}"/>
                                    <apex:actionSupport event="onchange" rerender="myFields"/>
                            </apex:selectList>
                     </apex:actionRegion>                         
              </apex:pageBlockSectionItem>

              <apex:pageBlockSectionItem >
                      <apex:outputlabel value="Field Names :"/>   
                      <apex:outputPanel id="myFields" layout="block">   
                        <apex:actionRegion >  
                           <apex:selectList value="{!selectedField}" size="10" multiselect="true">
                                <apex:selectOptions value="{!ObjectFields}"/>
                            </apex:selectList>
                        </apex:actionRegion>      
                     </apex:outputPanel>
              </apex:pageBlockSectionItem>

          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>
Chirag PaliwalChirag Paliwal
In addition to the Chosen Best Answer, I suppose you want to be able to use the date input by the user from the UI back in your controller, in order to do that use: 
Date dtMyDate = Date.Parse(stringDate);