2

Convert JSON date to javascript date in angularjs + asp.net how to convert json date /Date(1454351400000)/ into proper date format App.js file

 $http.get('/home/GetProducts')
    .success(function (result) {
        $scope.products = result;

    })
    .error(function (data) {
        console.log(data);
    });

index.cshtml file

<html>
<head>
    <title>Product with date</title>
    <script src="~/Scripts/angular.min.js"></script>
    <script src="~/Scripts/App.js"></script>   
</head>

<body ng-app="myApp">
    <h3>Products</h3>

    <div ng-controller="mainController" >

        <table class="table table-striped">
            <tr ng-repeat="product in products">
                <td>{{product.ProductName}}</td>

                <td>{{product.CreatedDate | date:"h:mma 'on' MMM d,y"}}</td>

                <td class="text-right">
                    <button class="btn btn-danger" ng-click="deleteProduct(product)">X</button>
                </td>
            </tr>
        </table>

        <input type="text" required ng-model="newProduct" />
        <a href="#" class="btn btn-primary" ng-click="addProduct()">Add Product</a>

    </div>
</body>
</html>
piyushdev
  • 19
  • 4
  • JSON is just a string representation of a plain javascript object. There is no such thing as a "JSON format date". You'll need to edit your post and let us know the accurate name for your dates format. – millerbr Feb 15 '16 at 08:05
  • Possible duplicate of [Converting .NET DateTime to JSON](http://stackoverflow.com/questions/1016847/converting-net-datetime-to-json) – karaxuna Feb 15 '16 at 08:35
  • if you think this has already solved please edit my file and send me.... thank you – piyushdev Feb 18 '16 at 11:59

1 Answers1

0

Please check this answer its worked for me.

HTML code

/* js */

'use strict';


filters.filter('jsonDate', function () {
    debugger;
  return function(input, format) {
    
    // Exit if the value isn't defined
    if(angular.isUndefined(input)) {
      return;
    }
    
    var date = new Date(parseInt(input.substr(6)));

    // John Pedrie added Moment.js support
    if(typeof moment !== 'undefined' && format) {
      var momentObj = moment(date);
      return momentObj.format(format);
    }
    else {
      return date.toLocaleDateString();
    }
  }
});
<td>{{ data.StartDate | jsonDate : 'DD-MM-YYYY' }} </td>