Magento Admin Grid Date Format: Adding a Calendar Picker
In this post, I’ll guide you through adding a calendar-style date picker to your Magento admin grid’s edit/adding screens. We’ll focus on the close_date column for a custom module named Party.
The Current State
As of now, your code for the grid edit and add pages shows a standard text field for the “Close Date” column:

You’ve tried setting the format to ‘date’ but haven’t seen any changes.
Understanding the Problem
The issue lies in how Magento handles date fields in admin forms. The date type you’re using is for dates with a specific format, not for interactive calendar pickers.
To create a calendar picker, we need to:
- Use the
datetimefield type. - Configure the form and grid properly to work together.
- Ensure the date is saved correctly in the database.
The Solution
Step 1: Modify the Form Field Type
In your module’s Foobar_Party/Block/Adminhtml/Party/Edit/Tabs/Form.php file, update the close_date field:
$fieldset->addField("party_close_date", "datetime", array(
"label" => Mage::helper("party")->__("Close Date"),
'name' => 'party_close_date',
));
Change date to datetime. This tells Magento to use a calendar picker instead of a simple text field.
Step 2: Update the Form and Grid
Ensure your form and grid are properly linked. The form should set the values from the session or registry, and the grid should display these dates correctly:
Form.php
class Foobar_Party_Block_Adminhtml_Party_Edit_Form extends Mage_Adminhtml_Block_Widget_Form {
// ... (prepareForm method continues as before)
}
Grid.php
Update the column definition and ensure you’re using the correct index name:
protected function _prepareColumns() {
/* Code for other columns */
$this->addColumn("party_close_date", array(
"header" => Mage::helper("party")->__("Close Date"),
"type" => "datetime",
"index" => "party_close_date", // Ensure the index matches the database column
));
/* ... (remaining code continues as before) */
}
Step 3: Save the Date in the Database
Make sure your Party model’s save() method handles dates correctly:
class Foobar_Party_Model_Party extends Mage_Core_Model_Abstract {
protected function _beforeSave() {
if ($this->getData('party_close_date') !== null) {
$this->setData('party_close_date', Varien_Date::date("Y-m-d H:i:s", $this->getData('party_close_date')));
}
return parent::_beforeSave();
}
// ... (remaining code continues as before)
}
The Final Result
After implementing these changes, your grid edit and add pages should now display a calendar picker for the “Close Date” field:

This way, you can easily select dates without entering them manually.
Conclusion
Adding a calendar-style date picker to your Magento admin grid involves understanding how Magento handles date fields and properly configuring the form and grid in your custom module. By following this guide, you’ll be able to create interactive date pickers for your admin screens.
For more advanced customizations and best practices in Magento development, check out Laravel Company’s blog.