Click here to Skip to main content
15,890,690 members
Articles / DateTime
Tip/Trick

Date Time Picker in ASP.NET Core using Jquery Plugin

Rate me:
Please Sign up or sign in to vote.
4.50/5 (6 votes)
30 Nov 2016CPOL3 min read 54.3K   3   4
In this tip, we will learn about using DateTime picker in ASP.NET Core using JQuery Plugin. Let’s start with a step by step approach.

Introduction

This tip explains how to add DateTime picker in ASP.NET Core MVC application using JQuery plug in.

There will be a situation where you require adding Date Picker, Date Time Picker in your application. Although you can use HTML5 controls for Date and Date Time Picker, it doesn’t look good on the page. So I will show you how to use JQuery Date Time picker plug in ASP.NET Core MVC application.

First, we will add HTML5 controls of type Date and DateTime and then later move on to adding DateTime picker using JQuery plug in ASP.NET MVC application which is simple, easy to use and looks good compared to the already existing HTML5 control.

Prerequisites

Install Visual Studio 2015 update 3 using anyone of the following link to work with .NET Core applications.

(Or)

Step 1: Create ASP.NET Core Web Application

Open Visual Studio and create a new project, select “.NET Framework 4.6.1” version and “ASP.NET Core Web application” from template. Give it a name of your choice and click on OK.

DateTime_Picker_New_App_ASPNET_CORE

Then select “Web Application” from ASP.NET Core templates and click OK to create the project.

DateTime_Picker_MVC_ASPNET_CORE

The below image is the project structure that will be created.

DateTime_Picker_MVC_ASPET_CORE_JQUERY_Project_Structure

Step 2: Create New Action Method in Controller

Open “Home“ controller and create new method and name it as “DateTime_Picker_Demo”.

DateTime_Picker_MVC_ASPNET_CORE__Action_Method

Then create an MVC view by selecting “MVC View” template.

DateTime_Picker_MVC_Core_Add_View

Add the following code to include the layout page for the created view:

HTML
@{
    ViewBag.Title = "DateTime Picker Demo";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Now let’s add built-in HTML5 Date control in the view and check the output:

HTML
<h2>Date Time Picker Demo</h2> 

<div class="container"> 
    <div class="row">
        <div>
            @Html.Label("HTML Date control", null, new { @class = "control-label col-xs-3" })
            <div class="col-xs-3">
                <input type="date" />
            </div>
        </div>
    </div>
</div>

Please find the below image for reference:

DateTime_Picker_MVC_ASPNET_CORE_View_Layout

Press F5 and see the output by changing the URL to the new action method created (eg:localhost:XXXXX/Home/DateTime_Picker_Demo)

DateTime_Picker_MVC_ASPNET_CORE_Click_First

Here in the above example, we have the option of selecting only Date, but not both Date and Time. So let’s add Date Time HTML5 control to the view and check the output.

Add the following code in the view inside the div container

HTML
<br />
    <div class="row">
        <div>
            @Html.Label("HTML DateTime control", null, 
            new { @class = "control-label col-xs-3" })
            <div class="col-xs-3">
                <input type="datetime-local" />
            </div>
        </div>
    </div>

Please find the below image:

HTML_DateTime_Picker_MVC_ASPNET_CORE

Press F5 and check the output.

DateTime_Picker_MVC_ASPNET_CORE_HTML_Second

The user can select both Date and Time from the control, but as discussed in the introduction, HTML5 controls for Date and Date Time Picker doesn’t really look good on the page. So our next task is to add Date Time picker using JQuery plug in which is simple, easy to use and looks good compared to the already existing HTML5 control.

Step 3: Let’s Add JQuery Date Time Picker plug in

Go to your Project solution and right click on “Bower” from “Dependencies” folder and select Manage Bower Packages from the options.

Then browse and search for “datetimepicker” in the search box and install the JQuery plug in as shown in the below image:

Bower_Date_Time_Picker_Jquery_Plug_in

Once you install the package, then you can see the JQuery DateTime Picker added under Bower and also the files under wwwroot ->lib folder as shown in the below image:

DateTime_Picker_MVC_ASPNET_CORE_JQUERY_Bower

Next step is to reference the added Date Time Picker JS and CSS files in the view page. Drag the files into the view and please note that the order of files being referenced should be followed as shown in the image.

DateTime_Picker_MVC_ASPNET_CORE_VIEW_Files

Now let’s see the code in the view, please add the following:

HTML
<div class="container">
    <div class="row">
       @Html.Label("DateTime using Jquery plug in",null, new { @class = "control-label col-sm-3" })
               <div class="col-sm-3"> 
                @Html.TextBox("datetimepicker")
               </div>
     </div>  
</div> 

<div class="row">
    <input type="submit" class="btn-primary" value="Submit"/>
    <input type="button" class="btn-danger" value="Cancel"/>
</div>

Now add the script in the view to refer to the date time picker:

HTML
<script type="text/javascript">  

   jQuery('#datetimepicker').datetimepicker();  

</script>

 

Press F5 and click on the textbox to see the output as shown in the below image:

DateTime_Picker_MVC_ASPET_CORE_JQUERY_ViewPage

There are several properties in the datetimepicker() method which you can use as per your requirement. I will show you some sample properties.

We can set the current date and time for the JQuery plug in when the page is loaded.

Example 1

//get today’s date using Date() method

HTML
var todaysdate = new Date();

    jQuery('#datetimepicker').datetimepicker({

        dayOfWeekStart: 1,

        lang: 'en',        

        startDate: todaysdate.getDate().toString(),

        startTime: todaysdate.getTime().toString()

    });

    jQuery('#datetimepicker').datetimepicker({ value: todaysdate.getDate().toString(), step: 10 });

Example 2

You set some dates as disabled by using “disabledDates” property.

HTML
disabledDates:['2016/01/11','2016/11/12','2016/11/27'],

disabled_dates_core_mvc

You can work with multiple properties provided by using this JQuery Date Time picker plug in as per your requirement.

Hope this will be helpful. :). Feel free to leave your comments below.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Deloitte
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionBower Missing Pin
mtoha23-Dec-21 14:36
professionalmtoha23-Dec-21 14:36 
QuestionTimePicker Pin
Nikos Karakatsidis3-Sep-18 5:00
Nikos Karakatsidis3-Sep-18 5:00 
QuestionMissing Pictures Pin
Dewey30-Nov-16 11:46
Dewey30-Nov-16 11:46 
AnswerRe: Missing Pictures Pin
Dudyalu Sravan Kumar Reddy30-Nov-16 23:46
Dudyalu Sravan Kumar Reddy30-Nov-16 23:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.