How to format ng-model string to date for angular material datepicker How to format ng-model string to date for angular material datepicker mongoose mongoose

How to format ng-model string to date for angular material datepicker


Here is an example:

Markup:

<div ng-controller="MyCtrl">    <md-datepicker ng-model="dt" md-placeholder="Enter date" ng-change="license.expirationdate = dt.toISOString()">    </md-datepicker>    {{license.expirationdate}}</div>

JavaScript:

app.controller('MyCtrl', function($scope) {    $scope.license = {        expirationdate: '2015-12-15T23:00:00.000Z'    };    $scope.dt = new Date($scope.license.expirationdate);});

Fiddle: http://jsfiddle.net/masa671/jm6y12un/

UPDATE:

With ng-repeat:

Markup:

<div ng-controller="MyCtrl">    <div ng-repeat="d in data">        <md-datepicker            ng-model="dataMod[$index].dt"            md-placeholder="Enter date"            ng-change="d.license.expirationdate = dataMod[$index].dt.toISOString()">        </md-datepicker>        {{d.license.expirationdate}}    </div></div>

JavaScript:

app.controller('MyCtrl', function($scope) {    var i;    $scope.data = [         { license:            { expirationdate: '2015-12-15T23:00:00.000Z' }        },        { license:            { expirationdate: '2015-12-20T23:00:00.000Z' }        },        { license:            { expirationdate: '2015-12-25T23:00:00.000Z' }        }    ];    $scope.dataMod = [];    for (i = 0; i < $scope.data.length; i += 1) {        $scope.dataMod.push({            dt: new Date($scope.data[i].license.expirationdate)        });    }});

Fiddle: http://jsfiddle.net/masa671/bmqpyu8g/


You can use ng-init, a custom filter, and ng-change and accomplish this in markup.

JavaScript:

app.filter('toDate', function() {    return function(input) {        return new Date(input);    }})

HTML:

<md-datepicker     ng-init="date = (license.expirationdate | toDate)"     ng-model="date"     ng-change="license.expirationdate = date.toISOString()"     md-placeholder="Enter date"></md-datepicker>

With this approach, you don't need to clutter your Controller code with View logic. The drawback is that any programmatic changes to license.expirationdate in the Controller will not be reflected in the View automatically.


http://jsfiddle.net/katfby9L/1/

// Configure the $httpProvider by adding our date transformerapp.config(["$httpProvider", function ($httpProvider) {    $httpProvider.defaults.transformResponse.push(function(responseData){        convertDateStringsToDates(responseData);        return responseData;    });}]);var regexIso8601 = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;function convertDateStringsToDates(input) {    // Ignore things that aren't objects.    if (typeof input !== "object") return input;    for (var key in input) {        if (!input.hasOwnProperty(key)) continue;        var value = input[key];        var match;        // Check for string properties which look like dates.        // TODO: Improve this regex to better match ISO 8601 date strings.        if (typeof value === "string" && (match = value.match(regexIso8601))) {            // Assume that Date.parse can parse ISO 8601 strings, or has been shimmed in older browsers to do so.            var milliseconds = Date.parse(match[0]);            if (!isNaN(milliseconds)) {                input[key] = new Date(milliseconds);            }        } else if (typeof value === "object") {            // Recurse into object            convertDateStringsToDates(value);        }    }}

This will automatically convert all strings in server JSON responses to date