Close
Angular React Web Components Blazor React
Open Source

React Date Time Input Overview

The Ignite UI for React Date Time Input allows the user to set and edit the date and time in a chosen input element. The user can edit both date and time portions using an editable masked input. Additionally, one can specify a desired display and input format, as well as min and max values to utilize validation.

Usage

First, you need to the install the corresponding Ignite UI for React npm package by running the following command:

npm install igniteui-react

You will then need to import the IgrDateTimeInput, its necessary CSS, like so:

import { IgrDateTimeInput } from 'igniteui-react';
import 'igniteui-webcomponents/themes/light/bootstrap.css';

Value binding

The easiest way to set the value of the IgrDateTimeInput component is by passing a Date object to the Value property:

<IgrDateTimeInput value={someValue}></IgrDateTimeInput>

The IgrDateTimeInput also accepts ISO 8601 strings.

The string can be a full ISO string, in the format YYYY-MM-DDTHH:mm:ss.sssZ or it could be separated into date-only and time-only portions.

Date-only

If a date-only string is bound to the Value property of the component, it needs to be in the format YYYY-MM-DD. The InputFormat is still used when typing values in the input and it does not have to be in the same format. Additionally, when binding a date-only string, the directive will prevent time shifts by coercing the time to be T00:00:00.

Time-only

Time-only strings are normally not defined in the ECMA specification, however to allow the directive to be integrated in scenarios which require time-only solutions, it supports the 24 hour format - HH:mm:ss. The 12 hour format is not supported.

Full ISO string

If a full ISO string is bound, the directive will parse it only if all elements required by Date.parse are provided.

All falsy values, including InvalidDate will be parsed as null. Incomplete date-only, time-only, or full ISO strings will be parsed as InvalidDate.

Keyboard Navigation

The IgrDateTimeInput has intuitive keyboard navigation that makes it easy to increment, decrement, or jump through different DateParts among others without having to touch the mouse.

KeysDescription
Move one character to the beginning
Move one character to the end
HOMEMove to the beginning
ENDMove to the end
CTRL / CMD + Move to the beginning of the date/time section - current one or left one
CTRL / CMD + Move to the end of the date/time section - current on or right one
Focus on a date/time part + Decrements a date/time part
Focus on a date/time part + Increments a date/time part
CTRL / CMD + ;Sets the current date/time as the value of the editor

Setting formats

The IgrDateTimeInput supports different display and input formats.

It uses Intl.DateTimeFormat which allows it to support predefined format options, such as long and short, medium and full. Additionally, it can also accept a custom string constructed from supported characters, such as dd-MM-yy. Also, if no DisplayFormat is provided, the component will use the InputFormat as such.

Input Format

The table bellow shows formats that are supported by the component’s InputFormat:

FormatDescription
dDate, will be coerced with a leading zero while editing.
ddDate with an explicitly set leading zero.
MMonth, will be coerced with a leading zero while editing.
MMMonth with an explicitly set leading zero.
yyShort year format.
yyyyFull year format.
hHours in 12-hour format, will be coerced with a leading zero while editing.
hhHours in 12-hour format with an explicitly set leading zero.
HHours in 24-hour format, will be coerced with a leading zero while editing.
HHHours in 24-hour format, with an explicitly set leading zero.
mMinutes, will be coerced with a leading zero while editing.
mmMinutes with an explicitly set leading zero.
ttAM/PM section for 12-hour format.

To set a specific input format, pass it as a string to the IgrDateTimeInput. This will set both the expected user input format and the mask. Additionally, the InputFormat is locale based, so if none is provided, the editor will default to dd/MM/yyyy.

<IgrDateTimeInput inputFormat="dd-MM-yy" displayFormat="medium"></IgrDateTimeInput>

If all went well, you should see the following in your browser:

Display format

The date time input exposes predefined formats for displaying date/time in various manners. All of the examples below are given in en-US locale.

OptionExample
short7/17/22, 12:00 AM
mediumJul 17, 2022, 12:00:00 AM
longJuly 17, 2022 at 12:00:00 AM GMT+3
fullSunday, July 17, 2022 at 12:00:00 AM Eastern European Summer Time
shortDate7/17/22
mediumDateJul 17, 2022
longDateJuly 17, 2022
fullDateSunday, July 17, 2022
shortTime12:00 AM
mediumTime12:00:00 AM
longTime12:00:00 AM GMT+3
fullTime12:00:00 AM Eastern European Summer Time

Furthermore, users can construct a displayFormat string using the supported symbols described in the following table.


TypeFormatDescriptionExample
DaydMinimum digits.7, 17
ddZero padded.07, 17
MonthMMinimum digits.3, 10
MMZero padded.03, 10
MMMAbbreviatedOct
MMMMWideOctober
MMMMMNarrowO
YearyNumeric2022
yyTwo digit22
yyyNumeric2022
yyyyNumeric2022
Hour 1-12hMinimum digits1, 12
hhZero padded01, 12
Hour 1-24HMinimum digits1, 23
HHZero padded01, 23
MinutemMinimum digits1, 59
mmZero padded01, 59
SecondsMinimum digits1, 59
ssZero padded01, 59
Time PeriodtAbbreviatedAM, PM
ttAbbreviatedAM, PM
tttShortnoon
ttttLongnoon
tttttNarrown

Many locales use the same time period string, irrespective of the format specified. Also, it has an effect only if a 12-hour clock is used.

Min/max value

You can specify Min and Max properties to restrict input and control the validity of the component. Just like the Value property, they can be of type string.

 <IgrDateTimeInput min={minDate} max={maxDate}></IgrDateTimeInput>

If all went well, the component will be invalid if the value is greater or lower than the given dates. Check out the example below:

Step up/down

The IgrDateTimeInput exposes public StepUp and StepDown methods. They increment or decrement a specific DatePart of the currently set date and time and can be used in a couple of ways.

In the first scenario, if no specific DatePart is passed to the method, a default DatePart will increment or decrement, based on the specified InputFormat and the internal component implementation. In the second scenario, you can explicitly specify what DatePart to manipulate as it may suite different requirements. Also, both methods accept an optional delta parameter of type number which can be used to set the stepUp/stepDown step.

Try it in the example below:

Styling

The IgrDateTimeInput component exposes CSS parts for almost all of its inner elements. The following table lists all of the exposed CSS parts:

NameDescription
containerThe main wrapper that holds all main input elements.
inputThe native input element.
labelThe native label element.
prefixThe prefix wrapper.
suffixThe suffix wrapper.
helper-textThe helper text wrapper.
igc-date-time-input {
    --_background: var(--ig-primary-200);
    --_foreground: var(--ig-gray-900);

    --input-prefix-color: var(--_foreground);
    --input-suffix-color: var(--_foreground);
    --input-prefix-color--filled: var(--_foreground);
    --input-suffix-color--filled: var(--_foreground);
    --input-prefix-background: var(--_background);
    --input-suffix-background: var(--_background);
    --input-prefix-background--filled: var(--_background);
    --input-suffix-background--filled: var(--_background);
    --input-prefix-color--focused: var(--_foreground);
    --input-suffix-color--focused: var(--_foreground);
    --input-prefix-background--focused: var(--_background);
    --input-suffix-background--focused: var(--_background);
}

API References

Additional Resources