55 */
66namespace Magento \Ui \Component \Form \Element \DataType ;
77
8+ use Exception ;
89use Magento \Framework \Locale \ResolverInterface ;
910use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
1011use Magento \Framework \View \Element \UiComponentInterface ;
1112use Magento \Framework \View \Element \UiComponent \ContextInterface ;
13+ use Magento \Framework \Stdlib \DateTime \Intl \DateFormatterFactory ;
14+ use Magento \Framework \App \ObjectManager ;
1215
1316/**
1417 * UI component date type
1518 */
1619class Date extends AbstractDataType
1720{
18- const NAME = 'date ' ;
21+ public const NAME = 'date ' ;
1922
2023 /**
2124 * Current locale
@@ -25,7 +28,7 @@ class Date extends AbstractDataType
2528 protected $ locale ;
2629
2730 /**
28- * Wrapped component
31+ * Wrapped component for date type
2932 *
3033 * @var UiComponentInterface
3134 */
@@ -36,6 +39,11 @@ class Date extends AbstractDataType
3639 */
3740 private $ localeDate ;
3841
42+ /**
43+ * @var DateFormatterFactory
44+ */
45+ private $ dateFormatterFactory ;
46+
3947 /**
4048 * Constructor
4149 *
@@ -44,17 +52,21 @@ class Date extends AbstractDataType
4452 * @param ResolverInterface $localeResolver
4553 * @param array $components
4654 * @param array $data
55+ * @param DateFormatterFactory|null $dateFormatterFactory
4756 */
4857 public function __construct (
4958 ContextInterface $ context ,
5059 TimezoneInterface $ localeDate ,
5160 ResolverInterface $ localeResolver ,
5261 array $ components = [],
53- array $ data = []
62+ array $ data = [],
63+ ?DateFormatterFactory $ dateFormatterFactory = null
5464 ) {
5565 $ this ->locale = $ localeResolver ->getLocale ();
5666 $ this ->localeDate = $ localeDate ;
5767 parent ::__construct ($ context , $ components , $ data );
68+ $ objectManager = ObjectManager::getInstance ();
69+ $ this ->dateFormatterFactory = $ dateFormatterFactory ?? $ objectManager ->get (DateFormatterFactory::class);
5870 }
5971
6072 /**
@@ -111,14 +123,15 @@ public function getComponentName()
111123 public function convertDate ($ date , $ hour = 0 , $ minute = 0 , $ second = 0 , $ setUtcTimeZone = true )
112124 {
113125 try {
126+ $ date = $ this ->convertDateFormat ($ date );
114127 $ dateObj = $ this ->localeDate ->date ($ date , $ this ->getLocale (), false , false );
115128 $ dateObj ->setTime ($ hour , $ minute , $ second );
116129 //convert store date to default date in UTC timezone without DST
117130 if ($ setUtcTimeZone ) {
118131 $ dateObj ->setTimezone (new \DateTimeZone ('UTC ' ));
119132 }
120133 return $ dateObj ;
121- } catch (\ Exception $ e ) {
134+ } catch (Exception $ e ) {
122135 return null ;
123136 }
124137 }
@@ -144,4 +157,29 @@ public function convertDatetime(string $date, bool $setUtcTimezone = true): ?\Da
144157 return null ;
145158 }
146159 }
160+
161+ /**
162+ * Convert given date to specific date format based on locale
163+ *
164+ * @param string $date
165+ * @return String
166+ * @throws Exception
167+ */
168+ public function convertDateFormat (string $ date ): String
169+ {
170+ $ formatter = $ this ->dateFormatterFactory ->create (
171+ $ this ->getLocale (),
172+ \IntlDateFormatter::SHORT ,
173+ \IntlDateFormatter::NONE ,
174+ date_default_timezone_get ()
175+ );
176+ $ formatter ->setLenient (false );
177+ if (!$ formatter ->parse ($ date )) {
178+ $ date = $ formatter ->formatObject (
179+ new \DateTime ($ date ),
180+ $ formatter ->getPattern ()
181+ );
182+ }
183+ return $ date ;
184+ }
147185}
0 commit comments