Table of contents
1.
Introduction
2.
Definition and Usage  
3.
Syntax
3.1.
Example
4.
Additional Attributes for Date Picker
4.1.
1. Min and Max Dates
4.2.
2. Default Value
4.3.
3. Required Field
4.4.
4. Step Attribute
5.
Approach
5.1.
Example: Display Selected Date
6.
How to Add a Date Picker in HTML?  
7.
Customizing the Date Picker
8.
How To Open HTML Date Picker Dialog On Click Anywhere Inside Input?  
9.
Browser Support  
10.
Frequently Asked Questions
10.1.
Why is my date picker not working in some browsers?
10.2.
How can I restrict users from selecting past dates?
10.3.
Can I use the date picker for selecting only months or years?
11.
Conclusion
Last Updated: Feb 9, 2025
Easy

How to Add a Date Picker in HTML?

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The date picker in HTML helps users select a date easily instead of typing it. You can add a date picker using the <input type="date"> element. This feature improves user experience by making date selection simple and reducing mistakes. However, the design of the date picker may look different in each browser. Some old browsers may not support it.

How to Add a Date Picker in HTML?

In this article, we will discuss the date picker in HTML, its syntax, attributes, and how to use it in different scenarios.

Definition and Usage  

The HTML date picker is a user interface element that allows users to select a date from a calendar. It is created using the `<input>` element with the `type="date"` attribute. When you use this input type, the browser automatically provides a calendar dropdown, making it easy for users to pick a date without manually typing it.  

The date picker is widely used in forms where date input is required. For example, it can be used for booking forms, event registration, or even for selecting a birthdate. The selected date is sent in the format `YYYY-MM-DD`, which is the standard date format.  

This is a basic example of how to create a date picker in HTML:  

<label for="birthday">Select your birthdate:</label>
<input type="date" id="birthday" name="birthday">


In this example:  

  • The `<label>` element is used to describe the purpose of the input field.  
     
  • The `<input>` element has `type="date"`, which tells the browser to render a date picker.  
     
  • The `id` attribute links the label to the input field, improving accessibility.  
     
  • The `name` attribute is used to identify the input field when the form is submitted.  


When you open this code in a browser, you’ll see a text box with a small calendar icon. Clicking on it will open a calendar where you can select a date.  

The HTML date picker is supported by most modern browsers, but its appearance & functionality may vary slightly depending on the browser. For example, some browsers might show a dropdown calendar, while others might display a pop-up calendar. 

Syntax

The date picker in HTML is implemented using the <input> tag with type="date". It creates a calendar-like UI, allowing users to select a date easily.

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Date Picker Example</title>
</head>
<body>
    <label for="dob">Select your Date of Birth:</label>
    <input type="date" id="dob" name="dob">
</body>
</html>

 

Output:

Output

This code creates a date input field where users can select a date from a built-in calendar.

Additional Attributes for Date Picker

The date picker can be customized using various attributes to control its behavior.

1. Min and Max Dates

You can restrict date selection within a specific range using min and max attributes.

Example:

<input type="date" id="event-date" name="event-date" min="2024-01-01" max="2024-12-31">

 

In this case, users can select a date only within the year 2024.

2. Default Value

Set a preselected date using the value attribute.

Example:

<input type="date" id="appointment" name="appointment" value="2024-06-15">

 

This sets the default date to June 15, 2024.

3. Required Field

Ensure users select a date by adding the required attribute.

Example:

<input type="date" id="check-in" name="check-in" required>

 

This field cannot be left empty when submitting a form.

4. Step Attribute

The step attribute specifies increments for selecting a date. The default step is 1 day, but it can be changed.

Example

<input type="date" id="weekly" name="weekly" step="7">

 

This allows only weekly date selections.

Approach

Let’s see how to integrate the date picker with JavaScript to enhance functionality.

Example: Display Selected Date

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Date Picker with JavaScript</title>
</head>
<body>
    <label for="meeting">Choose a meeting date:</label>
    <input type="date" id="meeting">
    <p id="output"></p>
    <script>
        document.getElementById('meeting').addEventListener('change', function() {
            let selectedDate = this.value;
            document.getElementById('output').innerText = "Selected Date: " + selectedDate;
        });
    </script>
</body>
</html>

 

Explanation:

  • The <input> field allows users to pick a date.
     
  • When a date is selected, an event listener captures the value.
     
  • The selected date is displayed below the input field dynamically.

How to Add a Date Picker in HTML?  

Adding a date picker in HTML is straightforward. You only need to use the `<input>` element with the `type="date"` attribute. This tells the browser to render a date picker interface. Let’s understand this in detail with a complete example.  

Let’s discuss how you can create a simple date picker:  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML Date Picker Example</title>
</head>
<body>
    <form>
        <label for="event-date">Select the event date:</label>
        <input type="date" id="event-date" name="event-date">
        <input type="submit" value="Submit">
    </form>
</body>
</html>


Output

Output

In this Code:  

1. HTML Structure:  

  • The `<!DOCTYPE html>` declaration defines the document type & version of HTML.  
     
  • The `<html>` tag is the root element of the HTML document.  
     
  • The `<head>` section contains metadata like the character set & viewport settings.  
     
  • The `<title>` tag sets the title of the webpage, which appears in the browser tab.  


2. Form Element:  

  • The `<form>` tag is used to create a form that can collect user input.  
     
  • Inside the form, we have a `<label>` element to describe the purpose of the input field.  
     

3. Date Picker Input:  

  • The `<input>` element with `type="date"` creates the date picker.  
     
  • The `id` attribute links the label to the input field for better accessibility.  
     
  • The `name` attribute is used to identify the input field when the form is submitted.  
     

4. Submit Button:  

  • The `<input>` element with `type="submit"` creates a button to submit the form.  

When you open this code in a browser, you’ll see a text box with a calendar icon. Clicking on the icon will open a calendar where you can select a date. After selecting a date, you can submit the form.  

Customizing the Date Picker

You can also set a minimum & maximum date range to restrict the dates users can select. For example:  

<label for="appointment">Select your appointment date:</label>
<input type="date" id="appointment" name="appointment" min="2023-10-01" max="2023-12-31">


In this example:  

  • The `min` attribute sets the earliest selectable date (October 1, 2023).  
     
  • The `max` attribute sets the latest selectable date (December 31, 2023).  
     

This ensures users can only select dates within this range.  

How To Open HTML Date Picker Dialog On Click Anywhere Inside Input?  

By default, the HTML date picker dialog opens only when you click the calendar icon or the input field itself. However, sometimes you might want the calendar to open when the user clicks anywhere inside the input field, not just the icon. This can be achieved using a bit of JavaScript.  

Let’s see how you can do it:  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Open Date Picker on Click Anywhere</title>
    <style>
        / Optional: Add some styling to the input field /
        input[type="date"] {
            padding: 10px;
            font-size: 16px;
            border: 1px solid ccc;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <form>
        <label for="meeting-date">Select the meeting date:</label>
        <input type="date" id="meeting-date" name="meeting-date">
    </form>
    <script>
        // JavaScript to open the date picker dialog on clicking anywhere inside the input field
        const dateInput = document.getElementById('meeting-date');
        dateInput.addEventListener('click', function() {
            this.showPicker();
        });
    </script>
</body>
</html>


Output

Output

In this Code:  


1. HTML Structure:  

  • The `<input>` element with `type="date"` creates the date picker.  
     
  • The `id` attribute is used to uniquely identify the input field.  

 

2. CSS Styling (Optional):  

  • The `<style>` block contains optional CSS to style the input field. This makes it look more user-friendly.  


3. JavaScript Functionality:  

  • The `<script>` block contains JavaScript code to enhance the date picker’s behavior. 
     
  • We use `document.getElementById('meeting-date')` to select the date input field.  
     
  • The `addEventListener` method listens for a `click` event on the input field.  
     
  • When the input field is clicked, the `showPicker()` method is called. This method programmatically opens the date picker dialog.  


How It Works:  

  • When the user clicks anywhere inside the input field, the `click` event is triggered.  
     
  • The `showPicker()` method is executed, which opens the date picker dialog.  
     
  • This makes the date picker more intuitive & user-friendly, as users don’t need to specifically click the calendar icon.  
     

Browser Compatibility:  

  • The `showPicker()` method is supported in most modern browsers, including Chrome, Firefox, & Edge.  
     
  • If you need to support older browsers, you might need to use a fallback solution or a JavaScript library.  

Browser Support  

The HTML date picker is widely supported in modern web browsers, but its appearance & functionality can vary slightly depending on the browser & its version. Let’s take a closer look at browser support & how to handle potential compatibility issues.  

Supported Browsers:  

The `<input type="date">` element is supported in the following browsers:  

  • Google Chrome: Fully supported.  
     
  • Mozilla Firefox: Fully supported.
      
  • Microsoft Edge: Fully supported.  
     
  • Safari: Fully supported on desktop & mobile.  
     
  • Opera: Fully supported.  

Frequently Asked Questions

Why is my date picker not working in some browsers?

The type="date" input is not supported in all browsers. Older versions of Safari and Internet Explorer may not support it. Consider using a JavaScript-based date picker like jQuery UI Datepicker for better compatibility.

How can I restrict users from selecting past dates?

You can use the min attribute to set the minimum allowed date. If you want to disable past dates dynamically, you can use JavaScript:

Can I use the date picker for selecting only months or years?

No, the default date picker does not support month or year selection. However, you can use type="month" for selecting months and type="week" for weeks.

Conclusion

In this article, we learned how to add a date picker in HTML using the <input type="date"> element and JavaScript-based libraries like jQuery UI and Flatpickr. A date picker enhances user experience by allowing easy date selection. Understanding these methods helps in building interactive and user-friendly web forms efficiently.

Live masterclass