Magento: Date format troubles

date_troubles © shadowkill@sxc.hu

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 🙂


7 comments

  1. Hi Darko

    We are developing a website for one of our clients who has a product list of more than 15000 products. The site is dealing in car parts and we have to give the “From” and “To” Model Year of the products. e.g. a specific product is suitable for a car model year from 1996 to 2000. Now when we do the search in Magento, it doesn’t recognize the year if we put it as 1997 or 1998 or 1999, It just shows the products if we enter the year 1996 or 2000 in the search criteria.

    For example the site owner is having a product suitable for a BMW 3 series made from the year 2010 to 2013. Now if in the search box, we add 2011 as the year, it doesn’t show the products. It only shows the related products if we select the year 2010 or 2013 and not for any year in between.

    Could you please suggest a solution for this.

    Regards

    Sajal

  2. Same issue on 1.7 CE when I’m trying to save CMS custom design info:

    – Custom Design From
    – Custom Design To

    – I’m trying with chrome
    – I have tried to change switch my locale (english/italian) but same issue

    The root of the problem looks to be the following one:

    Date picker fill dates in the format DD/MM/YY but validator.js (line 503) fail to create the new date(v) obj.

  3. @Tim, above problem was in EE, but CE doesn’t have customer attributes administration. Also, It would be best to trace your bug through source code to see what is happening. Please consider to give that task to company that handles and supports your Magento installation and I am sure that they will find the way to correct this. 🙂

  4. Hi Darko,
    Interesting read. I have a problem similar to your Issue 1.
    I have a module for adding Customer Attributes. My locale is UK so everything works in d/m/Y format. The calendar picker picks a date that show d/m/Y. But when you process the form (either registration or checkout) the day and month are transposed.
    This doesn’t happen on the admin side.

    I’ve been trying to track down the problem for ages.
    What module / setup are you using in this example?
    Is it CE with a module, or EE?
    I don’t have the “Input/Output Filter” option available in the module I’m using for customer attributes. Do you know what field that updates and what value does “Normalize Date” give the field?

    Thanks

  5. @Piotr, I would be glad to contribute to Magento 2, but for date issue #2 I mentioned, there is no need to change anything, becuse as I can see in Magento 2 source, this issue is already solved with:

    //...
    $element->setFormat(Mage::app()->getLocale()->getDateFormatWithLongYear());
    //...

    Cheers! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.