How To Set Date Format In Magento 2
There’s no surprise that Magento 2 is a global platform serving online businesses of all types. Magento 2 stores host visitors from all around the globe.
Every country does not follow the same date and time format. So, as per your Magento 2 store target audience, you need to configure the date and time options for the sake of customer convenience.
If you keep on using the default setting and the customers are using some different formats, it may confuse them.
Confusion is undoubtedly going to result in the visitors abandoning your store! We don’t want that.
So, it is ultra necessary to configure options like date & time, currencies, etc. as per the target customer base’s locale.
As a part of it, I’ve come up with two methods to set date format in Magento 2, as stated below:
Methods to Set Date Format in Magento 2:
From the Admin Panel:
- Login to admin panel
- Go to Stores > Configuration
- Under Catalog, select catalog
- Expand Date & Time Custom Options
- Use JavaScript Calendar – Set to “Yes” if you want to use the popup calendar for date fields.
- Date Fields Order – Set this field according to the format you require. For example, MM/DD/YY or DD/MM/YY
- Time Format – Choose the 12-hour format or the 24-hour format from the dropdown
- Year Range – Enter the year to set the from and to dates for the year range in the dropdown. If this field is left blank, it takes the default value as the current year.
- Save configuration
Programmatically:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php namespace Vendor\Extension\Model; use Magento\Framework\Stdlib\DateTime\DateTimeFactory; class ChangeDate { private $dateTimeFactory; public function __construct( DateTimeFactory $dateTimeFactory ) { $this->dateTimeFactory = $dateTimeFactory; } public function getDateByFormat($format) { $dateModel = $this->dateTimeFactory->create(); return $dateModel->gmtDate($format); } } |
Set custom date and time format in Magento 2 store using any of the above methods and offer excellent user experience.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
4 Comments
Hi Sanjay,
I also tried in same way from Admin to change the date format like ‘dd/mm/yyyy’, but it does not work in frontend. Can you please re-check this issue and confirm from your end.
Thanks.
Hello Biswajit,
We checked the solution and it works perfectly.
Thanks
I have tried to change the date format form the admin panel as mentioned in the blog. The date format I want is ‘dd/mm/yyyy’. But in the frontend, it still displaying as ‘mm/d/yyyy’.
Hello Fredy,
Are you checking in the custom form of Magento’s default form?
Also, please check if there are store wise different settings from the admin panel.
Thank you.