Magento: How to Add a Frontend Date Picker with DD/MM/YY Format
When working with Magento, you might encounter situations where you need to add a frontend date picker that returns dates in the DD/MM/YY format instead of the default MM/DD/YY. This can be achieved by modifying the calendar setup script and locale settings.
The Problem: US Date Format Override
Even if you have set your date format in the Magento backend to “dd/mm/yyyy” with an en_GB locale, you might still encounter US date formats when using the built-in calendar picker. This is because the core Magento files override the locale settings during script execution.
The Solution: Modify Calendar Script and Locale
To resolve this issue, we need to modify the calendar setup script and manually set the locale before initializing the calendar. Here’s a step-by-step guide:
Step 1: Update the Layout XML File
In your layout XML file (usually located in app/design/frontend/<your_package>/<your_theme>/layout), you should have a block definition like this:
<block type="core/html_calendar" name="html_calendar" as="html_calendar" template="page/js/calendar.phtml"/>
Make sure this block is included in your template file using the following code:
<?php echo $this->getChildHtml('html_calendar') ?>
Step 2: Override Calendar Script
Next, you need to override the core calendar script. Create a new JavaScript file (e.g., calendar-setup.js) in your theme directory at app/design/frontend/<your_package>/<your_theme>/js.
In this new file, include the original Magento calendar script and add the following modifications:
/* Override Magento Calendar Script */
(function ($) {
$.cal = {
setup: function () {
var locale = 'en-GB';
var format = 'dd/mm/yyyy';
var cal_img = $('.calendar_image');
cal_img.each(function (index, element) {
var id = $(this).attr('id');
var field = $('#' + id.replace('_trig', ''));
if (field.length > 0 && field.attr('class') == 'admin__field-calendar') {
new Calendar(field[0], { inputField: field, language: locale, showTime: false, allowBlank: true });
}
});
// ... Rest of the script remains unchanged
}
};
})(jQuery);
In this modified script:
- We define a
localevariable as ‘en-GB’ to set the UK English locale. - We define a
formatvariable as ‘dd/mm/yyyy’ to set the desired date format. - Before initializing each calendar instance, we manually set these variables.
Step 3: Include the Modified Script
Finally, update your header block in the layout XML file to include the new script:
<reference name="head">
<action method="addItem">
<type>js</type>
<name>calendar/calendar.js</name>
</action>
<action method="addItem">
<type>js</type>
<name>calendar/calendar-setup.js</name>
<params />
</action>
</reference>
Make sure the calendar-setup.js file is included after the core calendar.js file.
Conclusion
By following these steps, you should now have a Magento frontend date picker that returns dates in the DD/MM/YY format. The key modifications involve overriding the core calendar script and manually setting the locale before initializing each calendar instance.
Remember to clear your cache and refresh the page after making these changes. If you encounter any issues or require further assistance with Magento development, feel free to explore our resources at https://laravelcompany.com for in-depth guides and solutions tailored to your e-commerce needs.