Skip to content

Commit 4291ab2

Browse files
authored
Merge pull request #10561 from marmelab/DateTimeInput-parse
[Doc] Document how to return `Date` object with `<DateTimeInput>`
2 parents 71f4dbe + fba70e6 commit 4291ab2

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

docs/DateTimeInput.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { DateTimeInput } from 'react-admin';
2727

2828
The input value must be a valid date string, i.e. a string understood by JavasSript's [`Date.parse()` method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse), or a `Date` object. Strings with [the ISO 8601 format](https://en.wikipedia.org/wiki/ISO_8601) 'yyyy-MM-ddThh:mm' are the most common (e.g. `'2022-04-30T12:30'`). The field value may contain a timezone offset, e.g. `'2022-04-30T12:30+02:00'`. If no timezone is specified, the browser's timezone is used.
2929

30-
After modification by the user, the value is stored as a `Date` object, using the browser's timezone. When transformed to JSON, the date is serialized as a string in the ISO 8601 format ('yyyy-MM-ddThh:mm').
30+
After modification by the user, the value is stored as a string, using the same ISO 8601 format ('yyyy-MM-ddThh:mm').
3131

3232
**Tip**: For a Material UI styled `<DateTimeInput>` component, check out [MUI X Date Pickers](https://mui.com/x/react-date-pickers/)
3333

@@ -41,6 +41,17 @@ Internally, `<DateTimeInput>` renders an [`<input type="datetime-local">`](https
4141

4242
If you need to implement your own `format` and `parse` functions, make sure the **format** function actually formats the input into [a valid local date and time string](https://developer.mozilla.org/en-US/docs/Web/HTML/Date_and_time_formats#local_date_and_time_strings).
4343

44+
## Parsing as `Date` object
45+
46+
By default, `<DateTimeInput>` stores the date as a string in the form state. If you wish to store the date as a `Date` object instead, you can use the `parse` prop.
47+
48+
```tsx
49+
const parseDateTime = (value: string) =>
50+
value ? new Date(value) : value === '' ? null : value;
51+
52+
<DateTimeInput source="published" parse={parseDateTime} />
53+
```
54+
4455
## Material UI
4556

4657
[React-admin Enterprise Edition](https://react-admin-ee.marmelab.com)<img class="icon" src="./img/premium.svg" alt="React Admin Enterprise Edition icon" /> proposes an alternative `<DateTimeInput>` styled with Material UI.

packages/ra-ui-materialui/src/input/DateTimeInput.stories.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ export const ExternalChangesWithParse = ({
8181
</Wrapper>
8282
);
8383

84+
const parseDateTime = (value: string) =>
85+
value ? new Date(value) : value === '' ? null : value;
86+
87+
export const AsDateObject = () => (
88+
<Wrapper>
89+
<DateTimeInput source="published" parse={parseDateTime} />
90+
</Wrapper>
91+
);
92+
8493
const i18nProvider = polyglotI18nProvider(() => englishMessages);
8594

8695
const Wrapper = ({

0 commit comments

Comments
 (0)