0
votes

Can anyone explain why the Datepicker expects value to be date object as per the official example? https://material.angularjs.org/latest/demo/datepicker

To be honest this is a pain because before binding server response to the form I have to determine if a field is data type and convert the value:

$scope.myDate = new Date('2015-01-11');

Is there any way I could simply populate datepicker with a string value?

$scope.myDate = '2015-01-11'; 
1
Sorry if my question was unclear. I meant the standard HTML date picker accepts normalized date format YYYY-MM-DD e.g. <input type="date" name="bday" max="1979-12-31" value="1954-11-13"> It is extremely easy to bind to it JSON/AJAX value straight from the server response.Adam Bubela

1 Answers

0
votes

The problem with a string value would be parsing. May 10, 2016 and October 5, 2016 could be confused. 2016-05-10 or 2016-10-05. The date object protects against that. Can you not use a defined filter to convert your string data to a date object?

I quickly modified some code below from a Date Filter I have for Angular 1.x, which uses a numeric date format of YYYYMMDD (20160516).

/**
 * @name yourDate
 * @ngdoc filter
 * @requires $filter
 * @param DateValue {string} Date Value (YYYY-MM-DD)
 * @returns Date Filter with the Date Object
 * @description
 * Convert date from the format YYYY-MM-DD to the proper date object for future use by other objects/filters
 */
angular.module('myApp').filter('yourDate', function($filter)    {
        var DateFilter = $filter('date');
        return function(DateValue) {
            var Input = "";
            var ResultData = DateValue;
            if ( ! ( (DateValue === null) || ( typeof DateValue == 'undefined') ) ) {
                if ( Input.length == 10)    {
                    var Year = parseInt(Input.substr(0,4));
                    var Month = parseInt(Input.substr(5,2)) - 1;
                    var Day = parseInt(Input.substr(8, 2));
                    var DateObject = new Date(Year, Month, Day);

                    ResultData = DateFilter(DateObject);            // Return Input to the original filter (date)
                } else {
                }
            } else {
            }
            return ResultData;
        };
    }
);

/**
 * @description
 * Work with dates to convert from and to the YYYY-MM-DD format that is stored in   the system.
 */
angular.module('myApp').directive('yourDate',
    function($filter) {
        return {
            restrict: 'A',
            require: '^ngModel',
            link: function($scope, element, attrs, ngModelControl) {
                var slsDateFilter = $filter('yourDate');

                ngModelControl.$formatters.push(function(value) {
                    return slsDateFilter(value);
                });

                ngModelControl.$parsers.push(function(value) {
                    var DateObject = new Date(value);                   // Convert from Date to YYYY-MM-DD
                    return DateObject.getFullYear().toString() + '-' + DateObject.getMonth().toString() + '-' + DateObject.getDate().toString();
                });
            }
        };
    }
);

This code just uses the standard Angular Filter options, so you should then be able to combine this with your Material date picker.