Date picker not selected

I am using angular.js

MVC 5 in a web application. My web page has a data collector.

directive

myApp.directive('datepicker', function () {
return {
    restrict: 'A',
    require: 'ngModel',
    link: function (scope, element, attrs, ProfileCtrl) {
        element.datepicker({
            autoclose: true,
            onSelect: function (date) {
                ProfileCtrl.$setViewValue(date);
                scope.$apply();
            }
        });
    }
};});

      

input tag

<div class="form-group">
    <label for="dob" class="col-sm-4 control-label text-left">DOB</label>
    <div class="col-md-8">
       <input type="text" datepicker name="dob" class="form-control" ng-model="models.DOB" />
    </div>
 </div>

      

Everything works great.

enter image description here

but i am facing problem when i try to load date value from database into textbox, date picker does not highlight the selected date and dateformat date format is different from textbox. See the image below.

enter image description here

Any advice please help.

+3


source to share


1 answer


try this code to fix your date format and check if



    $scope.models.DOB=GetFormattedDate(models.DOB);
    function GetFormattedDate(dateString, replaceNullValue) {
         if (replaceNullValue == false && IsBlank(dateString)) {
           return "";
         }
        var dt = dateString != null ? new Date(dateString) : new Date();
        var d = dt.getDate();
        var dd = d < 10 ? "0" + d : d;
        var m = dt.getMonth() + 1;
        var mm = m < 10 ? "0" + m : m;
        var y = dt.getFullYear();
        return mm + "/" + dd + "/" + y;
    }

      

0


source







All Articles