5
5
*/
6
6
namespace Magento \Ui \Component \Form \Element \DataType ;
7
7
8
+ use Exception ;
8
9
use Magento \Framework \Locale \ResolverInterface ;
9
10
use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
10
11
use Magento \Framework \View \Element \UiComponentInterface ;
11
12
use Magento \Framework \View \Element \UiComponent \ContextInterface ;
13
+ use Magento \Framework \Stdlib \DateTime \Intl \DateFormatterFactory ;
14
+ use Magento \Framework \App \ObjectManager ;
12
15
13
16
/**
14
17
* UI component date type
15
18
*/
16
19
class Date extends AbstractDataType
17
20
{
18
- const NAME = 'date ' ;
21
+ public const NAME = 'date ' ;
19
22
20
23
/**
21
24
* Current locale
@@ -25,7 +28,7 @@ class Date extends AbstractDataType
25
28
protected $ locale ;
26
29
27
30
/**
28
- * Wrapped component
31
+ * Wrapped component for date type
29
32
*
30
33
* @var UiComponentInterface
31
34
*/
@@ -36,6 +39,11 @@ class Date extends AbstractDataType
36
39
*/
37
40
private $ localeDate ;
38
41
42
+ /**
43
+ * @var DateFormatterFactory
44
+ */
45
+ private $ dateFormatterFactory ;
46
+
39
47
/**
40
48
* Constructor
41
49
*
@@ -44,17 +52,21 @@ class Date extends AbstractDataType
44
52
* @param ResolverInterface $localeResolver
45
53
* @param array $components
46
54
* @param array $data
55
+ * @param DateFormatterFactory|null $dateFormatterFactory
47
56
*/
48
57
public function __construct (
49
58
ContextInterface $ context ,
50
59
TimezoneInterface $ localeDate ,
51
60
ResolverInterface $ localeResolver ,
52
61
array $ components = [],
53
- array $ data = []
62
+ array $ data = [],
63
+ ?DateFormatterFactory $ dateFormatterFactory = null
54
64
) {
55
65
$ this ->locale = $ localeResolver ->getLocale ();
56
66
$ this ->localeDate = $ localeDate ;
57
67
parent ::__construct ($ context , $ components , $ data );
68
+ $ objectManager = ObjectManager::getInstance ();
69
+ $ this ->dateFormatterFactory = $ dateFormatterFactory ?? $ objectManager ->get (DateFormatterFactory::class);
58
70
}
59
71
60
72
/**
@@ -111,14 +123,15 @@ public function getComponentName()
111
123
public function convertDate ($ date , $ hour = 0 , $ minute = 0 , $ second = 0 , $ setUtcTimeZone = true )
112
124
{
113
125
try {
126
+ $ date = $ this ->convertDateFormat ($ date );
114
127
$ dateObj = $ this ->localeDate ->date ($ date , $ this ->getLocale (), false , false );
115
128
$ dateObj ->setTime ($ hour , $ minute , $ second );
116
129
//convert store date to default date in UTC timezone without DST
117
130
if ($ setUtcTimeZone ) {
118
131
$ dateObj ->setTimezone (new \DateTimeZone ('UTC ' ));
119
132
}
120
133
return $ dateObj ;
121
- } catch (\ Exception $ e ) {
134
+ } catch (Exception $ e ) {
122
135
return null ;
123
136
}
124
137
}
@@ -144,4 +157,29 @@ public function convertDatetime(string $date, bool $setUtcTimezone = true): ?\Da
144
157
return null ;
145
158
}
146
159
}
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
+ }
147
185
}
0 commit comments