Open In App

React MUI MobileDateTimePicker API

React MUI is a UI library that provides fully-loaded components, bringing our own design system to our production-ready components. MUI is a user interface library that provides predefined and customizable React components for faster and easy web development, these Material-UI components are based on top of Material Design by Google.

The MobileDateTimePicker API is part of the Material-UI library for React. It provides a way to select a date and time on mobile devices with an intuitive and user-friendly interface. The API offers various options for customization and integration with other Material-UI components. 



Props List:

Slots List: Here is a list of the available slots in the MobileDateTimePicker component:



Syntax: The syntax of MobileDateTimePicker is as follows:

<MobileDateTimePicker
    label="For mobile"
    value={value}
    onChange={(newValue) => {
        setValue(newValue);
    }}
    renderInput={(params) => <TextField {...params} />}
/>

Installing React App:

Step 1: Create a React app using the following command.

npx create-react-app mobile-date-time-picker example

Step 2: Now get into the project directory.

cd mobile-date-picker-time-example

Installing Material-UI: Installing Material-UI’s source files via npm/yarn, and they take care of injecting CSS needed.

npm install @material-ui/core
Or
yarn add @material-ui/core

Installing the required package:

npm install @mui/material @emotion/react @emotion/styled
npm install @mui/x-date-pickers dayjs

Project Structure: 

Project Structure

Importing MobileDateTimePicker:

import { MobileDateTimePicker } from '@mui/x-date-pickers-pro/MobileDateTimePicker';
OR
import { MobileDateTimePicker } from '@mui/x-date-pickers/MobileDateTimePicker';
OR
import { MobileDateTimePicker } from '@mui/x-date-pickers-pro';
OR
import { MobileDateTimePicker } from '@mui/x-date-pickers';

Step to run the application: 

npm start

Example 1: 




import * as React from 'react';
import dayjs from 'dayjs';
import TextField from '@mui/material/TextField';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from 
    '@mui/x-date-pickers/LocalizationProvider';
import { DateTimePicker } from 
    '@mui/x-date-pickers/DateTimePicker';
import { MobileDateTimePicker } from 
    '@mui/x-date-pickers/MobileDateTimePicker';
import { DesktopDateTimePicker } from
    '@mui/x-date-pickers/DesktopDateTimePicker';
import Stack from '@mui/material/Stack';
  
export default function ResponsiveDateTimePickers() {
    const [value, setValue] = React
        .useState(dayjs('2023-03-02T00:00:00.000Z'));
  
    return (
        <LocalizationProvider dateAdapter={AdapterDayjs}>
            <h1 style={{ color: 'green' }}>GeeksForGeeks</h1>
            <h3>React MUI MobileDateTimePicker API</h3>
            <Stack spacing={3}>
                <MobileDateTimePicker
                    label="For mobile"
                    value={value}
                    onChange={(newValue) => {
                        setValue(newValue);
                    }}
                    renderInput={(params) => <TextField {...params} />}
                />
  
            </Stack>
        </LocalizationProvider>
    );
}

Output: 

Selecting the date and time

Example 2: We will see the ways to restrict date and time selection. There are two ways to do this:

  1. By using minDateTime or maxDateTime, it is possible to restrict the time selection to before or after a particular moment of time.
  2. By using minTime or maxTime, you can disable selecting times before or after a certain time each day respectively.




import * as React from 'react';
import dayjs from 'dayjs';
import TextField from '@mui/material/TextField';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from 
    '@mui/x-date-pickers/LocalizationProvider';
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
import Stack from '@mui/material/Stack';
  
export default function DateTimeValidation() {
    const [value, setValue] = React.useState(dayjs('2023-03-02'));
  
    return (
        <LocalizationProvider dateAdapter={AdapterDayjs}>
            <h1 style={{ color: 'green' }}>GeeksForGeeks</h1>
            <h3>React MUI MobileDateTimePicker API</h3>
            <Stack spacing={3}>
                <DateTimePicker
                    renderInput={(params) => <TextField {...params} />}
                    label="Ignore date and time"
                    value={value}
                    onChange={(newValue) => {
                        setValue(newValue);
                    }}
                    minDateTime={dayjs('2023-03-02T12:00')}
                />
                <DateTimePicker
                    renderInput={(params) => <TextField {...params} />}
                    label="Ignore time in each day"
                    value={value}
                    onChange={(newValue) => {
                        setValue(newValue);
                    }}
                    minDate={dayjs('2023-02-02')}
                    minTime={dayjs('2022-02-03T08:00')}
                    maxTime={dayjs('2022-02-03T18:45')}
                />
            </Stack>
        </LocalizationProvider>
    );
}

Output: 

 

Reference: https://mui.com/x/api/date-pickers/mobile-date-time-picker/


Article Tags :