How to set date format in HTML date input tag?

Spread the love

Sometimes, we want to set date format in HTML date input tag.

In this article, we’ll look at how to set date format in HTML date input tag.

How to set date format in HTML date input tag?

To set date format in HTML date input tag, we set the pattern attribute.

For instance, we write

<input
  type="date"
  name="input1"
  placeholder="YYYY-MM-DD"
  required
  pattern="[0-9]{4}-[0-9]{2}-[0-9]{2}"
/>

to set the pattern attribute to validate the date format wuth a regex.

We set it to a regex that matches the 4 digits, a hyphen, 2 digits, another hyphen, and another 2 digits.

Conclusion

To set date format in HTML date input tag, we set the pattern attribute.

Leave a Reply

Your email address will not be published. Required fields are marked *