I have an Angular collection bound to a repeater that is populated from an MVC JsonResult.
$scope.loadData = function () {
$http.get('/Product/GetDiscountCodes')
.then(function (result) {
console.log(result);
$scope.discountCodes = result.data.DiscountCodes;
$scope.selected = {};
});
};
One of the fields in the repeater is populated with the date. The date returned from the server is C# DateTime which looks like this:
1/5/2015 12:02:00 AM
However when it is bound by Angular is displays like this:
/Date(1420434120000)/
How can I get the date to display properly in my textfield in mm/dd/yyyy format?
I tried the ui-utils date format but that didn't work.
I also tried creating my own directive which formats the date properly but when the save event happens, the date send to the server side method is all jacked up.
function inputDate() {
return {
require: 'ngModel',
link: function (scope, element, attrs, ngModelController) {
ngModelController.$parsers.push(function (data) {
if (data === null) { return null; }
var d = moment(data).format('YYYY-MM-DD');
return d;
});
ngModelController.$formatters.push(function (data) {
if (data === null) { return null; }
return moment(data).format('MM/DD/YYYY');
});
}
}
}
angular.module('discountApp')
.directive('inputDate', inputDate);
THanks for the help!
use the {sampledate.slice(6, -2) | date:'dd/MM/yyyy'}
var app = angular.module('app',[]);
app.filter('ctime', function(){
return function(jsonDate){
var date = new Date(parseInt(jsonDate.substr(6)));
return date;
};
});
app.controller('fCtrl', function($scope){
$scope.date = '/Date(1420875802707)/';
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With