How To Use Bootstrap DateTime Picker In ASP.NET MVC With Angular As Directive


In this article, we will learn how to implement Bootstrap DateTime picker as Angular Directive in ASP.NET MVC.


We can create Bootstrap DateTime Picker in MVC easily. Now, if we use it normally with ASP.NET MVC, we will face some problems and DateTime Picker won't work as expected. In order to avoid that, we need to do it in the Angular way. So, we will create it as a directive and we can use it wherever we want in our project.

Steps to be followed

Step 1

Create an application and download the below CSS and JS files to add Bootstrap DateTime Picker.

  • bootstrap-datetimepicker.js
  • moment.js
  • angular.js
  • bootstrap-datetimepicker.css

You can download it from NuGet Package Manager using the command Install-Package Bootstrap.Datepicker -Version 1.6.4

Step 2

Refer the above-shown files in the View page.

Step 3

Create the HTML markup to show date time in the textbox.

  1. <div class='input-group date' id='datetimepicker1'>  
  2.                 <input type='text' class="form-control" ng-model="selectedDt" data-date-time-picker />  
  3.                 <span class="input-group-addon">  
  4.                     <span class="glyphicon glyphicon-calendar"></span>  
  5.                 </span>  
  6.             </div>  

Step 4

Create one module in JavaScript file or in script tag.

  1. var app = angular.module("Myapp", [])  

Create one directive named dateTimePicker and add to the app module.

  1. app.directive("dateTimePicker", DatetimePicker)  

Create one function for dateTimePicker directive and add the following code.

  1. function DatetimePicker() {  
  2.           return {  
  3.               restrict: "A",  
  4.               require: "ngModel",  
  5.               link: function (scope, element, attrs, ngModelCtrl) {  
  6.                   var parent = $(element).parent();  
  7.                   var dtp = parent.datetimepicker({  
  8.                       format: "DD-MM-YYYY hh:mm",  
  9.                       showTodayButton: true  
  10.                       //pickTime: true  
  11.                   });  
  12.                   dtp.on("dp.change"function (e) {  
  13.                       ngModelCtrl.$setViewValue(moment("DD-MM-YYYY hh:mm"));  
  14.                       scope.$apply();  
  15.                   });  
  16.               }  
  17.           };  
  18.       }  

In the above code, we are restricting the dateTimePicker directive to "Attribute only" and we will be using some “ngModel” for getting and setting the value of selected date time. Inside link function, we are manipulating the DOM and adding some bootstrap datetimepicker values to it. Also, we are adding datetimepicker change event in order to set the values to ngmodel.

Step 5

Create a Controller in script in order to bind the values to Model and show it in View.

  1. app.controller("ctrl"function ($scope) {  
  2.             $scope.test = "Angular Js Date time picker";  
  3.             $scope.selectedDt = "";  
  4.             $scope.showDate = function () {    //function to show date as alert  
  5.                 alert($scope.selectedDt);  
  6.             }  
  7.         })  

And, add this directive as an attribute to HTML markup.

  1. <input type='text' class="form-control" ng-model="selectedDt" data-date-time-picker />  

Add a click event to HTML in order to show the selected date.

  1. <button class="btn btn-success" ng-click="showDate()">Display selectedTime</button>  

Now, save it and run it. You will find the below output. You can reuse the same directive multiple times.