JavaScript

JavaScript Datepicker

The JavaScript “datepicker” permits you to pick a date from an interactive drop-down calendar rather than manually typing it. It is utilized in scenarios where you want to schedule tasks according to a specific date, such as saving a birthdate. A “datepicker” can be quickly added to an “input field” using the “jQuery Plugin“. Various other datepicker plugins also exist; however, “jQuery UI datepicker” stands out in the market because of its highly flexible nature.

This post will discuss the procedure to use a JavaScript datepicker. So, let’s start!

jQueryUI datepicker Plugin

The jQueryUI “datepicker” Plugin enables the user to enter the dates visually. It also allows you to easily change the date format and its language, add navigation options, and limit the date ranges. To utilize all of these functionalities, jQueryUI offers a “datepicker()” method and adds new CSS classes to HTML pages by changing their appearance.

Syntax

$(selector).datepicker(options);

Here, the “selector” parameter represent the CSS selectors such as “div” or any Document Object Model node, and “options” is an optional parameter that comprises different options such as “dateFormat”, “closeText”, “dayNames”.

Now, let’s head towards the implementation of JavaScript “datepicker”.

How to use JavaScript datePicker

For the demonstration purpose, we will create a JavaScript application and add the functionality of a basic “datepicker” to an input field. To do the same, follow the given step-by-step instructions:

Step 1: Import required libraries

First of all, import the required libraries in the HTML file for using jQuery UI plugin:

<link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

Step 2: Add HTML elements

In the next step, create the desired HTML elements in your application. In our case, we will firstly add a “title” within the “<head>” tag:

<head>

<title>JavaScript Datepicker</title>

</head>

Then, a heading with the “<h1>” tag and a paragraph with the “<p>” tag, inside of the “<body>” tag:

<body>

    <h1>Demonstration of using JavaScript Datepicker</h1>

    <p>Enter Date: <input type="text" id="datepicker"></p>

</body>

Also, in the added “input field”, an interactive calendar will appear in an overlay when the input field is focused.

Step 3: JavaScript file code

Switch to your JavaScript file and write out the following code to specify the input field id “#datepicker” as “selector” parameter of the “datepicker()” method. Here, you can also add some advanced configuration “options” related to JavaScript “datepicker” such as changing the date format:

$(function () {

    $("#datepicker").datepicker();

    dateFormat: "yy-mm-dd"

});

Step 4: CSS file code

To enhance the appearance of the JavaScript application, define the style of added HTML elements in the CSS file. For instance, we will add the background color for “body“, text color, and font-family for the “h1” heading and “p” paragraph elements:

body {

    background-color: powderblue;

}

h1 {

    color: rgb(21, 7, 145);

    font-family:'Courier New', Courier, monospace;

}

p {

    color: rgb(37, 25, 5);

    font-family:courier;

}

Step 5: Link CSS and JavaScript to HTML file

Lastly, link the CSS and JavaScript files to the JavaScript project HTML file:

<script src="datepicker.js"></script>

<link rel="stylesheet" href="datepicker.css">

Here is how our “JavaScript datepicker.html” file looks like:

Step 6: Run JavaScript application

After adding all of the required code, right-click on the HTML file, and from the opened context menu, select the “Open with Live Server” option:

Your created JavaScript application will now run in the browser. Click on the “input field” and choose a date from the “datepicker”:

We showed you the method to utilize a basic JavaScript datepicker. You can further explore as needed.

Conclusion

jQueryUI offers a “datepicker()” method that permits you to pick a date from an interactive drop-down calendar rather than manually typing it. A JavaScript datepicker is used in scenarios where you want to schedule tasks according to a specific date, such as saving a birthdate. In this post, we have discussed the method to add the functionality of a basic “datepicker” to an input field.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.