File tree 2 files changed +26
-1
lines changed
packages/ra-ui-materialui/src/field
2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,19 @@ describe('<DateField />', () => {
31
31
expect ( queryByText ( date ) ) . not . toBeNull ( ) ;
32
32
} ) ;
33
33
34
+ it ( 'should render a date string' , ( ) => {
35
+ const { queryByText } = render (
36
+ < DateField
37
+ record = { { id : 123 , foo : '2017-04-23' } }
38
+ source = "foo"
39
+ locales = "en-US"
40
+ />
41
+ ) ;
42
+
43
+ const date = new Date ( '2017-04-23' ) . toLocaleDateString ( 'en-US' ) ;
44
+ expect ( queryByText ( date ) ) . not . toBeNull ( ) ;
45
+ } ) ;
46
+
34
47
it ( 'should use record from RecordContext' , ( ) => {
35
48
const { queryByText } = render (
36
49
< RecordContextProvider
Original file line number Diff line number Diff line change @@ -71,12 +71,24 @@ export const DateField: FC<DateFieldProps> = memo(props => {
71
71
}
72
72
73
73
const date = value instanceof Date ? value : new Date ( value ) ;
74
+ let dateOptions = options ;
75
+ if (
76
+ typeof value === 'string' &&
77
+ value . length <= 10 &&
78
+ ! showTime &&
79
+ ! options
80
+ ) {
81
+ // Input is a date string (e.g. '2022-02-15') without time and time zone.
82
+ // Force timezone to UTC to fix issue with people in negative time zones
83
+ // who may see a different date when calling toLocaleDateString().
84
+ dateOptions = { timeZone : 'UTC' } ;
85
+ }
74
86
const dateString = showTime
75
87
? toLocaleStringSupportsLocales
76
88
? date . toLocaleString ( locales , options )
77
89
: date . toLocaleString ( )
78
90
: toLocaleStringSupportsLocales
79
- ? date . toLocaleDateString ( locales , options )
91
+ ? date . toLocaleDateString ( locales , dateOptions )
80
92
: date . toLocaleDateString ( ) ;
81
93
82
94
return (
You can’t perform that action at this time.
0 commit comments