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
RustyboyRustyboy 

DatePicker format changes when VF page refreshed

Apologies if this has already been answered but I have spent more than an hour looking and still cannot find an answer to my problem:

 

I am using a datepicker on an apex:inputtext field, to establish a from and to date range. It works fine, but when the screen is refreshed the value in the date changes from:

 

   D/MM/YYY format (what I am after), to something like this: 

   Mon Jul 15 00:00:00 GMT 2013

 

Therefore, next time I attempt to perform an action, I get an error message saying:

 

   Error:   Value 'Mon Jul 15 00:00:00 GMT 2013' cannot be converted from Text to Date
 
How can I prevent the date from being incorrectly reformatted?
 
Here is an exceprt from my VF code:
 

<apex:pageBlockSectionItem >
   <apex:outputLabel value="From Due Date" for="fromDate"/>
   <apex:inputText value="{!fromDateCriteria}" size="10" onfocus="DatePicker.pickDate(false, this, false);" id="fromDate" />
</apex:pageBlockSectionItem>

<apex:pageBlockSectionItem >

 

<apex:pageBlockSectionItem >
   <apex:outputLabel value="To Due Date" for="toDate"/>
   <apex:inputText value="{!toDateCriteria}" size="10" onfocus="DatePicker.pickDate(false, this, false);" id="toDate" />
</apex:pageBlockSectionItem>

 

 

My controller simply uses auto getters and setters:

 

public date FromDateCriteria {get;set;}
public date ToDateCriteria {get;set;}

 

Everything work fine, except for the reformat when the UI is redisplayed.

 

Thanks in advance...

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

my mistake!!!!

 

 

private Date FromDateCriteria;
private Date ToDateCriteria;

public void setFromDateCriteria(Date FromDateCriteria) {
    this.FromDateCriteria = FromDateCriteria;
}
public String getFromDateCriteria() {
    //you can put a null check here  
    return FromDateCriteria.format();
}

public void setToDateCriteria(Date ToDateCriteria) {
    this.ToDateCriteria = ToDateCriteria;
}
public String getToDateCriteria() {
    //you can put a null check here   
    return ToDateCriteria.format();
}

All Answers

Neeraj_ChauriyaNeeraj_Chauriya

Hi,

Try formatting the date fields in the getters:

public date FromDateCriteria
{
    get{
        return FromDateCriteria.format('yyyy-MM-dd');
    }set;
}
public date ToDateCriteria {
    get{
        return ToDateCriteria.format('yyyy-MM-dd');
    }set
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other's benefit.

RustyboyRustyboy

I thought you had the solution then,when I tried it I got the following error message:

 

Error: Compile Error: Method does not exist or incorrect signature: [Date].format(String) at line 19 column 21

 

I tried to do it as a DateTime field instaed, but then it insisted on having a time component too.

 

Any other ideas?

Avidev9Avidev9

Rusty boy you are close!!!!

 

Format method of Date class automatically formats the date to user locale. 

 

public date FromDateCriteria
{
    get{
        return FromDateCriteria.format();
    }set;
}
public date ToDateCriteria {
    get{
        return ToDateCriteria.format();
    }set
}
RustyboyRustyboy

Hi Avi: Still does not work. I get:

 

Error: Compile Error: Return value must be of type: Date at line 27 column 13

 

 

RustyboyRustyboy

PS: Also made a change to convert to a DateTime field and return newDate.Date() - same problem as before, with the date format when the page is refreshed

Avidev9Avidev9

lets do it a different way,

 

private Date FromDateCriteria;
private Date ToDateCriteria;

public void setFromDateCriteria(Date FromDateCriteria) {
    this.FromDateCriteria = FromDateCriteria;
}
public date getFromDateCriteria() {
    //you can put a null check here  
    return FromDateCriteria.format();
}

public void setToDateCriteria(Date ToDateCriteria) {
    this.ToDateCriteria = ToDateCriteria;
}
public date getToDateCriteria() {
    //you can put a null check here   
    return ToDateCriteria.format();
}

 

RustyboyRustyboy

Thanks for sticking with this Avi :o)

 

The problem with using format() is that it returns a string, but FromDateCriteria is a date, so I get this message:

 

Error: Compile Error: Return value must be of type: Date at line 106 column 9

 

Code here for reference:

 

public date FromDateCriteria;

 

public date getFromDateCriteria() {

        if (FromDateCriteria==null) {
            return null;
        }
        return FromDateCriteria.format();
}

Avidev9Avidev9

my mistake!!!!

 

 

private Date FromDateCriteria;
private Date ToDateCriteria;

public void setFromDateCriteria(Date FromDateCriteria) {
    this.FromDateCriteria = FromDateCriteria;
}
public String getFromDateCriteria() {
    //you can put a null check here  
    return FromDateCriteria.format();
}

public void setToDateCriteria(Date ToDateCriteria) {
    this.ToDateCriteria = ToDateCriteria;
}
public String getToDateCriteria() {
    //you can put a null check here   
    return ToDateCriteria.format();
}
This was selected as the best answer
RustyboyRustyboy

Works perfectly - thanks Avi

FL blondieFL blondie
When I try this
public date FromDateCriteria;

public date getFromDateCriteria() {
        if (FromDateCriteria==null) {
            return null;
        }
        return FromDateCriteria.format();
}

I get .... Compile Error: Return value must be of type: Date.  How else can I fix this?
vamsi krishna 106vamsi krishna 106
 Public Date implanteddate;
    public void setimplanteddate(Date implanteddate) {
    this.implanteddate= implanteddate;
}
public String getimplanteddate() {
     if(implanteddate==NULL)
     {
     return NULL;
     }
    return implanteddate.format();
}


Hey Guys i tried like this ..but in vf page i am getting   this error Read only property  'implanteddate'  please help me guys...Thanks advance
abs2310abs2310
Given the field fromDateCriteria is already a Date type in Controller there is no need to show the field as inputText and specify onclick DatePicker to it. Instead of using the DatePicker change the tag to use type="date" and Salesforce will automatically show date along with date picker tied to it.
Before
<apex:inputText value="{!fromDateCriteria}" size="10" onfocus="DatePicker.pickDate(false, this, false);" id="fromDate" />
After
<apex:input type"date" size="10" value="{!fromDateCriteria}" id="fromDate" />