Angular Material DatePicker date.toLocaleDateString is not a function

I try to use the angular date picker and I get the following error:

TypeError: date.toLocaleDateString is not a function
at Object.defaultFormatDate [as formatDate] (angular-material.js:6860)
at DatePickerCtrl.configureNgModel.ngModelCtrl.$render (angular-material.js:7184)
at Object.<anonymous> (angular.js:25233)
at m.a.$get.m.$digest (angular.js:15707)
at m.a.$get.m.$apply (angular.js:15986)
at g (angular.js:10511)
at L (angular.js:10683)
at XMLHttpRequest.A.onload (angular.js:10624)

I gave an example from the code: https://material.angularjs.org/latest/#/demo/material.components.input

Corresponding HTML code:

            <div layout="" layout-sm="column">
            <md-input-container style="width:70%">
                <label>Company (Disabled)</label>
                <input ng-model="user.company" disabled="">
            </md-input-container>

            <md-datepicker ng-model="date" md-placeholder="Enter date"></md-datepicker>
        </div>

My controller

$scope.date={};
$scope.$watch('date', function () {
    console.log($scope.date);
});
+4
source share
4 answers

toLocaleDateString is a native method of a date object, so it seems that your md-datepicker is not bound to a date. Without HTML and code that populates your data, it's hard to say for sure, but this is the most likely reason.

+4
source

, date date, , , string - .

. , .

0

, , md-datepicker, javascript

$scope.eventdate = $filter('date')($scope.eventdate, 'yyyy-MM-dd');

md-datepicker ,

js

$scope.eventdate = $filter('date')($scope.mddate, 'yyyy-MM-dd');

html

 <md-datepicker ng-model="mddate" md-placeholder="Enter date" required></md-datepicker>

,

-1

, .

Initializing the datepicker using the new Date (), but when trying to initialize with the value returned from the server, I got the error above - despite the fact that they seem to be in the same format.

In the end, I had to explicitly create a Date object and then set its value using Parse.

        var date = ctrl.data.fromServer;
        ctrl.data.fromServer= new Date();
        if (date != undefined)
        {
            ctrl.data.fromServer.setTime(Date.parse(date));
        }
-1
source

Source: https://habr.com/ru/post/1606519/


All Articles