简体   繁体   中英

Angularjs doesn't change datepicker date format

I tried many solutions from this site marked as anwers still the datepicker doesn't change the date format. i'm using this angular directive with no results:

App.directive('datepicker', function() {
       return function(scope, element, attrs) {
           element.datepicker({
               inline: true,
               dateFormat: 'dd/mm/yy',
               onSelect: function(dateText) {
                   var modelPath = $(this).attr('ng-model');
                   putObject(modelPath, scope, dateText);
                   scope.$apply();
               }
           });
       }
    });

also on the html:

 <input type="text" ng-model="finalDate" date-format="dd/mm/yyyy" date-type="string" datepicker>

nothing either, added this code in the script:

  $scope.$watch('datepicker.date', function(v){ // using the example model from the datepicker docs
        var d = new Date(v);
        var curr_date = d.getDate();
        var curr_month = d.getMonth() + 1; //Months are zero based
        var curr_year = d.getFullYear();
        $scope.modDate = curr_date + "/" + curr_month + "/" + curr_year;
        console.log($scope.modDate)
    })

the console log prints nothing. Honestly i don't know what else to do, i tried other solutions with no result. any ideas?

EDIT: Plunker added http://plnkr.co/edit/hoGYFslN3KovH0wkj5kE?p=preview

Use this directive

App.directive('datepicker', ['$filter', '$parse', function ($filter, $parse) {
return {
    require: 'ngModel',
    link: function (scope, element, attrs, ctrl) {

        element.datetimepicker({
            weekStart: 1,
            todayBtn: 1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            minView: 2,
            forceParse: 0,
            endDate: attrs.enddate == "" ? new Date() : null,
            format: "yyyy-MM-dd"
        }).on('changeDate', function (ev) {
            var dateUTC = new Date(ev.date.getTime() + (ev.date.getTimezoneOffset() * 60000));
            var filterApply = $filter('date')(dateUTC, 'yyyy-MM-dd');
            $parse(attrs.ngModel).assign(scope, filterApply);
            scope.$digest();
        });
    }
}}]);

if you want to use your directive, it should be like

App.directive('datepicker', function ($filter, $parse) {
return function (scope, element, attrs) {
    element.datepicker({
        inline: true,
        dateFormat: 'dd/mm/yy',
        onSelect: function (dateText) {
            var dateUTC = new Date(dateText.date.getTime() + (dateText.date.getTimezoneOffset() * 60000));
            var filterApply = $filter('date')(dateUTC, 'dd/mm/yy');
            $parse(attrs.ngModel).assign(scope, filterApply);
            scope.$digest();
        }
    });
}});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM