---
title: "How to get the Current Date using Custom Format in Magento 2"
url: "https://magecomp.com/blog/get-the-current-date-using-custom-format-in-magento-2/"
date: "2019-10-21T18:06:54+00:00"
modified: "2025-02-21T09:21:06+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 303
reading_time: "2 min read"
summary: "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 dat..."
description: "How to get the Current Date using Custom Format in Magento 2"
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to create Barcode of invoice ID and add it to Magento 2 invoice PDF?"
    url: "https://magecomp.com/blog/create-barcode-invoice-id-add-magento-2-invoice-pdf/"
  - title: "How to Get CMS Page List in System Configuration in Magento 2?"
    url: "https://magecomp.com/blog/cms-page-list-in-system-configuration-in-magento-2/"
  - title: "Magento 2: How to Display Product Image on Admin Order Create Page"
    url: "https://magecomp.com/blog/magento-2-display-product-image-on-admin-order-create-page/"
---

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

_Published: October 21, 2019_  
_Author: Dhiren Vasoya_  

![How to get the current date using custom format in Magento 2](https://magecomp.com/blog/wp-content/uploads/2019/10/How-to-get-the-current-date-using-custom-format-in-Magento-2-1024x332.webp)

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.

[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-1.webp)](https://magecomp.com/services/magento-upgrade-services/)
**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\**

```

<?php

namespace VendorExtensionControllerGetformatteddate;

use MagentoFrameworkStdlibDateTimeDateTimeFactory;

class Index extends MagentoFrameworkAppActionAction
{

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

    private $dateTimeFactory;

    public function __construct(
        MagentoFrameworkAppActionContext $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();
    }
}
```

[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-5.webp)](https://magecomp.com/magento-malware-removal.html)

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.

[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-1.webp)](https://magecomp.com/magento-performance-optimization.html)
Happy Fixing!


---

_View the original post at: [https://magecomp.com/blog/get-the-current-date-using-custom-format-in-magento-2/](https://magecomp.com/blog/get-the-current-date-using-custom-format-in-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-07-21 05:30:38 UTC_  
