8

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!

2
  • That is somewhat of a hack that Microsoft uses to get the date. I think they do that to get around the annoying UTC offsets. But anyways just evaluate the Date() function in your service call. If you do not want to evaluate it you could use extract the timestamp and use momentjs. Point is mold that data in the service before returning it to any other services or controllers. Commented Apr 30, 2015 at 2:19
  • I use a filter in these cases. This answer uses a moment.js filter. My usage looks like: <span>{{myDate | jsonDate: "MM-dd-yyyy"}}</span> Commented Apr 30, 2015 at 2:33

4 Answers 4

10

use the {sampledate.slice(6, -2) | date:'dd/MM/yyyy'}

Sign up to request clarification or add additional context in comments.

1 Comment

This should be the best answer
8
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)/';

});

1 Comment

how do we use this filter? using | ctime? It didnt work for me. Could you please elaborate?
2

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

Comments

-1

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'" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.