diff --git a/vendor/magento/module-ui/Component/Form/Element/DataType/Date.php b/vendor/magento/module-ui/Component/Form/Element/DataType/Date.php index 3600992011ed..ca5e295492fe 100644 --- a/vendor/magento/module-ui/Component/Form/Element/DataType/Date.php +++ b/vendor/magento/module-ui/Component/Form/Element/DataType/Date.php @@ -5,17 +5,20 @@ */ namespace Magento\Ui\Component\Form\Element\DataType; +use Exception; use Magento\Framework\Locale\ResolverInterface; use Magento\Framework\Stdlib\DateTime\TimezoneInterface; use Magento\Framework\View\Element\UiComponentInterface; use Magento\Framework\View\Element\UiComponent\ContextInterface; +use Magento\Framework\Stdlib\DateTime\Intl\DateFormatterFactory; +use Magento\Framework\App\ObjectManager; /** * UI component date type */ class Date extends AbstractDataType { - const NAME = 'date'; + public const NAME = 'date'; /** * Current locale @@ -25,7 +28,7 @@ class Date extends AbstractDataType protected $locale; /** - * Wrapped component + * Wrapped component for date type * * @var UiComponentInterface */ @@ -36,6 +39,11 @@ class Date extends AbstractDataType */ private $localeDate; + /** + * @var DateFormatterFactory + */ + private $dateFormatterFactory; + /** * Constructor * @@ -44,17 +52,21 @@ class Date extends AbstractDataType * @param ResolverInterface $localeResolver * @param array $components * @param array $data + * @param DateFormatterFactory|null $dateFormatterFactory */ public function __construct( ContextInterface $context, TimezoneInterface $localeDate, ResolverInterface $localeResolver, array $components = [], - array $data = [] + array $data = [], + ?DateFormatterFactory $dateFormatterFactory = null ) { $this->locale = $localeResolver->getLocale(); $this->localeDate = $localeDate; parent::__construct($context, $components, $data); + $objectManager = ObjectManager::getInstance(); + $this->dateFormatterFactory = $dateFormatterFactory ?? $objectManager->get(DateFormatterFactory::class); } /** @@ -111,6 +123,7 @@ public function getComponentName() public function convertDate($date, $hour = 0, $minute = 0, $second = 0, $setUtcTimeZone = true) { try { + $date = $this->convertDateFormat($date); $dateObj = $this->localeDate->date($date, $this->getLocale(), false, false); $dateObj->setTime($hour, $minute, $second); //convert store date to default date in UTC timezone without DST @@ -118,7 +131,7 @@ public function convertDate($date, $hour = 0, $minute = 0, $second = 0, $setUtcT $dateObj->setTimezone(new \DateTimeZone('UTC')); } return $dateObj; - } catch (\Exception $e) { + } catch (Exception $e) { return null; } } @@ -144,4 +157,30 @@ public function convertDatetime(string $date, bool $setUtcTimezone = true): ?\Da return null; } } + + /** + * Convert given date to specific date format based on locale + * + * @param string $date + * @return String + * @throws Exception + */ + public function convertDateFormat(string $date): String + { + $formatter = $this->dateFormatterFactory->create( + $this->getLocale(), + \IntlDateFormatter::SHORT, + \IntlDateFormatter::NONE, + date_default_timezone_get() + ); + + $formatter->setLenient(false); + if (!$formatter->parse($date)) { + $date = $formatter->formatObject( + new \DateTime($date), + $formatter->getPattern() + ); + } + return $date; + } }