How to convert date according to specific locale in Magento 2?
How to convert date according to specific locale in Magento 2?

How to convert date according to specific locale in Magento 2?

Sometimes we need to convert date formate to locale. We can use \Magento\Framework\Stdlib\DateTime\TimezoneInterface class to convert dare according to locale.

Using Object Manager:

$date = new \DateTime('2019-11-21');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$formatDate = $objectManager->create('Magento\Framework\Stdlib\DateTime\TimezoneInterface')
    ->formatDate(
        $date,
        \IntlDateFormatter::FULL,
        false
    );

Using Dependency Injection:

namespace MagePrince\Testing\Model;

class FormatDatLocale
{
    private $timezoneInterface;

    public function __construct(
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezoneInterface
    ) {
        $this->timezoneInterface = $timezoneInterface;
    }

    public function getFormatDate()
    {
        $date = new \DateTime('2019-11-21');
        
        $formatDate = $this->timezoneInterface->formatDate(
            $date,
            \IntlDateFormatter::FULL,
            false
        );

        return $formatDate;
    }
}

Hope this will help you. If you have any question please comment below. Thank you for reading. Happy Coding…:)