When I use a date field in a VF page and try to do validation and return a validation error - such as, if date can't be greater than today's date, the date format automatically changes to something like,

Jul 20 2015 00:00:00

and when I try to submit that again, I get an error that states that values can't be converted from text to date. Even when I try to do any formatting within the controller and assign it back to the date field as mm/dd/yyyy, it still only shows in the above format.

Does anyone know how to keep the date format as mm/dd/yyyy or whatever format is acceptable when the post back occurs and I do not get this error.

Right now, I'm using a apex:input "text field" and using the date.parse function to convert it into a date before assigning to the sobject's field before I save it to the database.

Is this the correct way of doing it? or is there an easier workaround?

<apex:page controller="contExtBkup" docType="html-5.0">
<apex:pageMessages />
<apex:includeScript value="{!URLFOR($Resource.D2DCalendar, '/calendar.js')}" />
 <apex:stylesheet value="{!URLFOR($Resource.D2DCalendar, '/calendar.css')}"/>

<apex:form >
<apex:pageblock >
    <apex:pageBlockSection >
                     <apex:pageBlockSectionItem >
                     <apex:outputLabel value="{!$ObjectType.contact.fields.birthdate.label}"  />
                      <apex:outputPanel styleClass="requiredInput" layout="block">
                      <apex:outputPanel layout="block" styleClass="requiredBlock" />
                            <apex:inputtext id="birthdate"   

                        <apex:outputPanel rendered="{!dobOp}">
                                <apex:inputtext id="birthdate1" styleClass="error"  


                                <div class="errorMsg"><strong>Error:</strong>&nbsp;{!dob_error}</div>


<apex:pageBlockButtons >
    <apex:commandButton value="Next" action="{!next}"  />



Note I'm including an inline Javascript calendar here


 public with sharing class contExtBkup {
 public boolean dobOp{get;set;}
 public string dob_Error{get;set;}
 public Contact contact{get;set;}

 public contExtBkup() {
        if(this.contact == null)
            this.contact = new Contact();

public pageReference next(){

    if (contact.birthdate == null){
        dob_Error = 'Please enter date';

     else if (contact.birthdate >= date.today()){
            dob_Error='DOB cant be current/future date';
                       /*string date1 = contact.Birthdate.format();
            contact.Birthdate = date.parse(date1);
            contact.Birthdate = date.parse('07/31/2015'); */
  return page.datePickerJS;

enter image description here}

I have added the error message too...How to change this format to something like MM/DD/YYYY so that I don't get the error message about converting text to date.

  • Give a try to your date format something like this - MM/dd/yyyy
    – MIX DML
    Aug 3, 2015 at 19:18
  • any reason why you are not using <apex:inputField> tag?
    – Aayush K
    Aug 3, 2015 at 19:20
  • @Prabhat - Right, that's what I tried in my controller code, string date1 = contact.Birthdate.format(); contact.Birthdate = date.parse(date1); return page.datePickerJS; but it's still not working.
    – JJSHHS
    Aug 3, 2015 at 19:20
  • @AAU - I'm getting the above problem when I use apex:inputfield tag. Hence, I had to use apex:input as an alternative but I'm asking in this forum if someone has run into this issue and if so, is there an acceptable workaround
    – JJSHHS
    Aug 3, 2015 at 19:22
  • I even tried hardcoding the date as '07/31/2015' just to see what would happen, but it always convert into Jul 31 2015 00:00:00 this format.
    – JJSHHS
    Aug 3, 2015 at 19:26

2 Answers 2


If you change your inputtext to inputField, then it will work. In this case you would not need the onfocus="initialiseCalendar(...)"

<apex:inputField id="birthdate"   

Otherwise, if you want to use a custom date picker, then change your inputtext to input and add type="date"

<apex:input id="birthdate"
  • That's what I have done, but I'm not really sure if it's the "correct" way to do it. - like what @crop1645 said. Hence, I wanted to make sure
    – JJSHHS
    Aug 6, 2015 at 4:44

Use Date class of Apex. That will help.

You can use it like this:-


public class ControllerForDate {
    public Time useDate {get; set;}
    public ControllerForDate()
        if (useDate == null) {
           useDate =  DateTime.now().date();

In Page, use this:-

<apex:input type="date" value="{!useDate}"/>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .