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!
using code script:
$filter('date')($scope.Your_DateTime_Model, 'MM/dd/yyyy')
using code view:
<input class="form-control" type="text" data-ng-model="Your_DateTime_Model | date:'MM/dd/yyyy'" />
use the {sampledate.slice(6, -2) | date:'dd/MM/yyyy'}
This seems to be issue with the serialization library you are using in .Net. Look at this blog post by Scott Hanselman
http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx
It talks about this very issue. Date should be serialized as
ISO 8601
format.You may have to change your serializer, see here Setting the Default JSON Serializer in ASP.NET MVC