Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

<input type="date">

BaselineWidely available

<input> elements oftype="date" create input fields that let the user enter a date. The appearance of the date picker input UI varies based on the browser and operating system. The value is normalized to the formatyyyy-mm-dd.

The resulting value includes the year, month, and day, butnot the time. Thetime anddatetime-local input types support time and date+time input.

Try it

<label for="start">Start date:</label><input  type="date"   name="trip-start"  value="2018-07-22"  min="2018-01-01"  max="2018-12-31" />
label {  display: block;  font:    1rem "Fira Sans",    sans-serif;}input,label {  margin: 0.4rem 0;}

Value

A string representing the date entered in the input. The date is formatted according toDate strings format.

You can set a default value for the input with a date inside thevalue attribute, like so:

html
<input type="date" value="2017-06-01" />

Note:The displayed date format will differ from the actualvalue — the displayed date is formattedbased on the locale of the user's browser, but the parsedvalue is always formattedyyyy-mm-dd.

You can get and set the date value in JavaScript with theHTMLInputElementvalue andvalueAsNumber properties. For example:

js
const dateControl = document.querySelector('input[type="date"]');dateControl.value = "2017-06-01";console.log(dateControl.value); // prints "2017-06-01"console.log(dateControl.valueAsNumber); // prints 1496275200000, a JavaScript timestamp (ms)

This code finds the first<input> element whosetype isdate, and sets its value to2017-06-01 (June 1st, 2017). It then reads that value back in string and number formats.

Additional attributes

In addition toglobal attributes and theinput attributes common to all<input> elements, thedate input supports the following attributes:

max

The latest date to accept. If thevalue entered into the element occurs afterward, the element failsconstraint validation. If the value of themax attribute isn't a possible date string in the formatyyyy-mm-dd, then the element has no maximum date value.

If both themax andmin attributes are set, this value must be a date stringlater than or equal to the one in themin attribute.

min

The earliest date to accept. If thevalue entered into the element occurs beforehand, the element failsconstraint validation. If the value of themin attribute isn't a possible date string in the formatyyyy-mm-dd, then the element has no minimum date value.

If both themax andmin attributes are set, this value must be a date stringearlier than or equal to the one in themax attribute.

step

Thestep attribute is a number that specifies the granularity that the value must adhere to, or the special valueany, which is described below. Only values which are equal to the basis for stepping (min if specified,value otherwise, and an appropriate default value if neither of those is provided) are valid.

A string value ofany means that no stepping is implied, and any value is allowed (barring other constraints, such asmin andmax).

Note:When the data entered by the user doesn't adhere to the stepping configuration, theuser agent may round to the nearest valid value, preferring numbers in the positive direction when there are two equally close options.

Fordate inputs, the value ofstep is given in days; and is treated as a number of milliseconds equal to 86,400,000 times thestep value (the underlying numeric value is in milliseconds). The default value ofstep is 1, indicating 1 day.

Note:Specifyingany as the value forstep has the same effect as1 fordate inputs.

Using date inputs

Date inputs provide an easy interface for choosing dates, and they normalize the data format sent to the server regardless of the user's locale.

In this section, we'll look at basic and then more complex uses of<input type="date">.

Basic uses of date

The most basic use of<input type="date"> involves one<input> combined with its<label>, as seen below:

html
<form action="https://example.com">  <label>    Enter your birthday:    <input type="date" name="bday" />  </label>  <p><button>Submit</button></p></form>

This HTML submits the entered date under the keybday tohttps://example.com — resulting in a URL likehttps://example.com/?bday=1955-06-08.

Setting maximum and minimum dates

You can use themin andmax attributes to restrict the dates that can be chosen by the user. In the following example, we set a minimum date of2017-04-01 and a maximum date of2017-04-30:

html
<form>  <label>    Choose your preferred party date:    <input type="date" name="party" min="2017-04-01" max="2017-04-30" />  </label></form>

The result is that only days in April 2017 can be selected — the month and year parts of the textbox will be uneditable, and dates outside April 2017 can't be selected in the picker widget.

You can use thestep attribute to vary the number of days jumped each time the date is incremented (e.g., to only make Saturdays selectable).

Controlling input size

<input type="date"> doesn't support form sizing attributes such assize. PreferCSS for sizing it.

Validation

By default,<input type="date"> doesn't validate the entered value beyond its format. The interfaces generally don't let you enter anything that isn't a date — which is helpful.

If you usemin andmax to restrict the available dates (seeSetting maximum and minimum dates), the form control disables invalid dates, and will display an error if you try to submit a date that is out of bounds.

You can also use therequired attribute to make filling in the date mandatory — an error will be displayed if you try to submit an empty date field.

Let's look at an example of minimum and maximum dates, and also make a field required:

html
<form>  <label>    Choose your preferred party date (required, April 1st to 20th):    <input      type="date"      name="party"      min="2017-04-01"      max="2017-04-20"      required />    <span></span>  </label>  <p>    <button>Submit</button>  </p></form>

If you try to submit the form with an incomplete date (or with a date outside the set bounds), the browser displays an error. Try playing with the example now:

Here's the CSS used in the above example. We make use of the:valid and:invalidpseudo-elements to add an icon next to the input, based on whether the current value is valid. We had to put the icon on a<span> next to the input, not on the input itself, because in Chrome at least the input's generated content is placed inside the form control, and can't be styled or shown effectively.

css
label {  display: flex;  align-items: center;}span::after {  padding-left: 5px;}input:invalid + span::after {  content: "✖";}input:valid + span::after {  content: "✓";}

Warning:Client-side form validationis not a substitute for validating on the server. It's easy for someone to modify the HTML, or bypass your HTML entirely and submit the data directly to your server. If your server fails to validate the received data, disaster could strike with data that is badly-formatted, too large, of the wrong type, etc.

Examples

In this example, we create a date picker using the native<input type="date"> picker.

HTML

The HTML looks like so:

html
<form>  <div>    <label for="bday">Enter your birthday:</label>    <input type="date" name="bday" />    <span></span>  </div></form>

CSS

The CSS looks like so:

css
input:invalid + span::after {  content: " ✖";}input:valid + span::after {  content: " ✓";}

Results

Technical summary

Value A string representing a date in YYYY-MM-DD format, or empty
Eventschange andinput
Supported common attributesautocomplete,list,readonly,step
IDL attributesvalue,valueAsDate,valueAsNumber
DOM interfaceHTMLInputElement
Methodsselect(),stepDown(),stepUp()
Implicit ARIA Roleno corresponding role

Specifications

Specification
HTML
# date-state-(type=date)

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp