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
Steve NelsonSteve Nelson 

Formatting apex:inputField value attribute for Date datatype

Hello

Newbie here. I fear this is a bit basic, but I've been looking for answers for a couple of weeks with no luck. Hopefully this isn't too much to ask of the community.

​I have a custom object and a Visualforce page to edit the records. There is a Due Date field on the object of type Date.  When the record is edited, we need the date that is displayed in the input field to appear either in US format, or according to the user's locale.  We are fine with either. What we always get is yyyy-mm-dd. We want mm/dd/yyyy. Have read that Visualforce is supposed to respect a user's locale settings.

There are lots of posts around date formatting when you are using apex:outputText and apex:param tags when you are just viewing the data, but that doesn't work when you have an apex:inputField tag.

Here is the code:

<apex:page standardController="MyObject__c" docType="html-5.0" >
 <apex:form>
  <apex:outputPanel layout="block" >
   <apex:outputLabel value="Due Date" for="duedate"/>
   <apex:inputField type="date" showDatePicker="false" styleClass="slds-input" id="duedate" value="{!MyObject__c.Due_Date__c}"/>
  </apex:outputPanel>
 </apex:form>
</apex:page>

​​I have found a few approaches that deal with some custom controller code. But it doesn't seem like it should be that difficult or that much work. Is there some easy formatting option that I am missing? The code above is easy with the simple binding right to the object field. I did some experimenting trying to override the field in a controller extension to format a string using a getter. But I couldn't get that exactly right from the perspective of how to reference the controller extension's methods in the page, let alone what to do during a save operation. But if doing it in controller code is necessary, I'd appreciate some pointers in the right direction. I'm going through Trailhead and some other educational materials as I can, but in parallel have some development requests to address.

Thanks!
Steve​


Best Answer chosen by Steve Nelson
john4sfdcjohn4sfdc
Hi Steve,
try removing the doctype="html 5.0" and type="date" component in apex input field tag. You can use the below code for reference


<apex:page standardController="Opportunity">
<apex:outputText value="{0,date,MM/dd/yy}">
<apex:param value="{!opportunity.CloseDate}" />
</apex:outputText> </apex:page>
 

All Answers

sfdcsushilsfdcsushil
Steve,

Based on your code, it seems you dont to display datepicker? You just need text field to enter date? Or you want to use html5 datepicker?

If you dont use type attribute of html 5, it should respect your locale as per my understanding - example below
<apex:inputField  id="duedate"  value="{!MyObject__c.Due_Date__c}"/>
john4sfdcjohn4sfdc
Hi Steve,
try removing the doctype="html 5.0" and type="date" component in apex input field tag. You can use the below code for reference


<apex:page standardController="Opportunity">
<apex:outputText value="{0,date,MM/dd/yy}">
<apex:param value="{!opportunity.CloseDate}" />
</apex:outputText> </apex:page>
 
This was selected as the best answer
Steve NelsonSteve Nelson
Thanks your your help.

It seems the type="date" attribute was the problem.  I have removed that and the format is correct. Is there an explanation?  It would seem we would want to apply data types where possible.

The html-5.0 attribute is still in the apex:page tag. The end result of the app will be to apply the Lightning Design System, including the date picker the design system supplies. So, I believe the html-5.0 attribute needs to remain.  Correct?​​  And does the removal of the type="date" attribute now factor into using that date picker?

I will mark this as solved, but I'd appreciate any final thoughts on the questions in this post.​
sfdcsushilsfdcsushil
basd on my knowledge, Inputfield functionality is standard salesforce, so sfdc takes care of everything for you. If you supply type parameter, it overrides sfdc picker and uses html5. 
Please check this thread, it should give you some explanation - 

http://​http://stackoverflow.com/questions/2968817/is-there-a-way-to-localize-input-type-date-in-html5