Magento: Date format troubles

In this post, I will just mention some of the issues with date format from my own experience, in hope that this will help somebody to save few hours of tracing Magento to find the solution :-).

Issue 1:

Indian site doesn’t allow to save customer from customer edit screen inside adminhtml, it says that DOB (Date Of Birth) format is incorrect:

Currently for that site, date format used “en_US” as we can see from admin configuration and inside lib/Zend/Locale/Data/en.xml it looks like this:

<dateformatlength type="short">
<dateformat>
<pattern>M/d/yy</pattern>
</dateformat>
</dateformatlength>

Let’s say that we want to choose 30th March as Date of Birth.

We don’t need to write the date, but we can pick it from date picker javascript widget.

After we pick 30 March, our Date Of Birth field looks like this:

03/30/76 as it supposed to be.

But when we try to save the customer, it generates an error message.

“Date Of Birth” does not fit the entered date format.

So, what’s wrong?

After some debugging and researching, I found that Magento reverses the date and it tries to save it like 3rd day of 30th month which is of course the thing that causes the error on date validation. I also found that we forgot to set input/output filter of Customer Date Attribute to “Normalize Date” value and it caused the problem.

Solution:

Navigate to Customers -> Attributes -> Manage Customer Attributes. Search for attribute with code “dob” and click on displayed row to edit attribute properties.

Set Input/Output filter to “Normalize Date” value and save the attribute.

Date properties screen should now look like this:

Now let’s try to edit our customer Date Of Birth again and it works!

Issue 2

When working on German site, we discovered some strange behavior of DOB customer attribute.

In Magento adminhtml on Customer edit screen we have a field named “Date of Birth”.

The field as whole Magento installation was set to German localization and date in this field was in format:

//lib/Zend/Locale/Data/de.xml
 
<dateformatlength type="short">
<dateformat>
<pattern>dd.MM.yy</pattern>
</dateformat>
</dateformatlength>

What happened was: If we set some customer Date of birth for example:

01/12/56

the question is what year is saved inside database?

Is it 1956 or 2056 ?

Unfortunately, I found that saved year is 2056.

So, what can we do to resolve this issue? We wanted to get full date from database and display it on frontend as pre-selected date field on customer account, so we had to fix this issue somehow …

One of possible solutions was to override Magento’s functionality and implement different date format for calendar widget in adminhtml, to get full year number in date input.

We can do that by overriding Mage_Adminhtml_Block_Widget_Form::_setFieldset method and instead:

//...
} else if ($inputType == 'date') {
$element->setImage($this->getSkinUrl('images/grid-cal.gif'));
$element->setFormat(
Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)
);
//...

We can put this:

//...
} else if ($inputType == 'date') {
$element->setImage($this->getSkinUrl('images/grid-cal.gif'));
$element->setFormat(
Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_LONG)
);
//...

After refreshing customer edit screen in Magento admin, we can see that DOB date format has changed.

Also, please be aware that now we also changed the other fields that uses this method to render date picker ….

Cheers 🙂