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

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

2 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

4 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

6 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago