Fix for the Datefield issue in flex 2.0

Sunday, 22 July 2007, 12:09 | Category : Flex
Tags :

There seems to be an issue with the <mx:DateField> component in Flex 2.0. The issue is that when you choose todays date i.e. the current date which is highlighted grey in the DateChooser again the selected date clears out and you get a null as the selected date. Even if you select any other date two times in the chooser its becomes null and the text clears out. I am not sure why on earth Adobe is clearing out the date. Let the user select any date he wants to and let programmer’s brain do the rest 😉 . This is an issue and the workaround i coded for the above is as follows.

Let’s take an example:

<mx:DateField id=”dfStartDate” selectedDate=”{DateRange.getDate(xxx)}”
editable=”true” change=”updateStartDate(event)”/>

private function updateStartDate(event : CalendarLayoutChangeEvent) : void {
if (event.newDate != null) {
var dateNew : Date = new Date(event.newDate.getFullYear(), event.newDate.month,;
dfStartDate.selectedDate = event.newDate;

This function gets the changed date and sets it back to the dateField so that we do not get null on getting the selectedDate and also the datefield does not clear out.flex date field

But the problem i am facing now is whenever the user changes the date the text in the datefield shifts left for no good reason and the date text is not readable. i have to again focus in the field to make it readable. Please suggest a workaround.

Aaah !!! i finally managed to fix the date field width issue by setting the width to 75 i.e. just set the width=”75″ or greater and the value will not shift to left.

  • Viral

    good job brother,
    i am facing same problem while using DateField as a ItemRenderer in DataGrid.
    i tried to use your code but getting some strange ‘selectedDate’ property not found type error.

    please have a look @ below code, if u find some time. as i am new to FLEX, i hv no much idea in this.

    thanks n regards,

  • Suzanne

    Thanks for the width solution. I had to set mine to 100 before it worked. Point is, making the width a little larger than the width of the date text will prevent it from shifting left. Thanks for the help!