In my application the max date is calculated based on some logic. The date is a JSON date. It will be used to set the max date.
To show the problem I have created a plunker link (http://plnkr.co/edit/KR2brhOXPMgZhbhafhex?p=preview).
In my example max-date
attribute gets the JSON date
<input type="text" date-picker="" date-range="true" ng-model="Date"
class="form-control input-sm" placeholder="MM/DD/YYYY"
ng-required="true" name="Date" ng-pattern='datePattern'
max-date="dateLimit"/>
In my controller I try to convert the JSON date to mm/dd/yy date string. For this I create a function getcalculateddDate
that takes in
Evaluated my date in chrome dev tool console
app.controller('MainCtrl', function($scope) {
$scope.datePattern = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/i
var jsonDateExample = "/Date(1522728000000)/";
$scope.dateLimit = getCalculatedDate(jsonDateExample );
function getCalculatedDate(date) {
var year = date.getFullYear();
var month = (1 + date.getMonth()).toString();
month = month.length > 1 ? month : '0' + month;
var day = (date.getDate() - 1).toString();
day = day.length > 1 ? day : '0' + day;
return month + '/' + day + '/' + year;
}
});
I have created a directive to help me in achieving this
app.directive('datePicker', function () {
return {
require: 'ngModel',
scope: {
dateRange: '=',
maxDate: '='
},
link: function (scope, el, attr, ngModel) {
if (scope.dateRange !== undefined && scope.dateRange) {
$(el).datepicker({
maxDate: scope.maxDate,
minDate: '-1M',
onSelect: function (dateText) {
scope.$apply(function () {
ngModel.$setViewValue(dateText);
});
}
});
}
else {
$(el).datepicker({
onSelect: function (dateText) {
scope.$apply(function () {
ngModel.$setViewValue(dateText);
});
}
});
}
}
};
});