Back to Top

Convert Date According to Specific Locale Magento 2

Updated 16 July 2021

Convert Date According to Specific Locale Magento 2 : Today we will learn how to convert a date to a specific locale.
So magento has an interface DateTimeFormatterInterface, through which we can convert a date to any other locale according to our need.
check the below code.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$dateTimeFormatter = $objectManager->get('Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface');
$staticDate = new \DateTime('2012-07-31');
/**
 * Returns a translated and localized date string
 *
 * @param \IntlCalendar|\DateTime $object
 * @param string|int|array|null $format
 * @param string|null $locale
 * @return string
 */
$convertedDate = $dateTimeFormatter->formatObject($staticDate, \IntlDateFormatter::FULL, 'es_AR');

So convertedDate variable contains translated and localized date string.

Hope so it will help.

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • Erfan
    • ashutosh srivastava (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home