Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert C# DateTime To Angular Date

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!

like image 380
Mike Anderson Avatar asked Apr 30 '15 02:04

Mike Anderson


2 Answers

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

like image 79
vijayakumar v.p Avatar answered Sep 19 '22 16:09

vijayakumar v.p


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)/';

});
like image 38
Abhishek Dey Avatar answered Sep 18 '22 16:09

Abhishek Dey