How To

How to get the Current Date using Custom Format in Magento 2

Date and time is essential element for any business to keep track of their data over a different time frame. But when it comes to any CMS or platform, it does provide the functionality to fetch date and time using its default variable without a single line of code and the same thing applied to Magento 2. By default, you can set system date and time using its backend system configuration that you want to use in Magento 2. So, whenever you fetch date and time it uses its default type as shown below.
2019-10-21 18:15:30
But while custom development, many times it happens that you have to fetch date and time in a specific format. Also, Magento 2 is not providing an option to fetch date in a different format. So, you have to manually code for it.

To do the same first you need to create an “Index.php” file using the following code at the below path.
app\code\Vendor\Extension\Controller\Getformatteddate\

<pre class="lang:default decode:true">
<?php

namespace Vendor\Extension\Controller\Getformatteddate;

use Magento\Framework\Stdlib\DateTime\DateTimeFactory;

class Index extends \Magento\Framework\App\Action\Action
{

    const FORMAT_DATE = 'd-m-Y H:i:s';//set format as per your requirement

    private $dateTimeFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        DateTimeFactory $dateTimeFactory)
    {
        $this->dateTimeFactory = $dateTimeFactory;
        return parent::__construct($context);
    }
    public function getCurrentDate()
    {
        $dateModel = $this->dateTimeFactory->create();
        return $dateModel->gmtDate(self::FORMAT_DATE);
    }
    public function execute()
    {
        echo "Current Date : ".$this->getCurrentDate();
    }
}
</pre>

That’s it. You are free to manipulate this code according to your needs.
Lastly, if you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends and Let us know if you are facing any issue while implementing this code.
Happy Fixing!

Click to rate this post!
[Total: 10 Average: 4.6]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

19 hours ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

20 hours ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

2 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

4 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

4 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

5 days ago