0

In my Sql Server I have a column that has a date data type and here is a sample 2017-09-28. I created a model with this as display format:

[Display(Name = "Enroll Date")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime CreationDate { get; set; }

and a controller in my angular controller:

    var app = angular.module("MyApp", []);

app.controller("PdfController", function($scope, $http) {
    $scope.message = "";
    $scope.records = null;
    $http({
        method: 'GET',
        url: '/Pdf/GetData'
    }).then(function(result) {
            $scope.records = result.data;
        },
    function(error) {
        $scope.message = "Error in retrieving data @error: " + error;
    });
})

then in my view, I used the ng-repeat like so:

<tr ng-repeat="record in records">
    <td>{{record.Id}}</td>
    <td>{{record.RfidNumber}}</td>
    <td>{{record.RfidTag}}</td>
    <td>{{record.AccountName}}</td>
    <td>{{record.CreationDate | date:'dd/MM/yyyy'}}</td>
    <td>{{record.EncodedBy}}</td>
</tr>

Why is it that in my table I get this value /Date(1506528000000)/ and I cannot get it to be formatted to dd/MM/yyyy? I tried setting my model to be public string CreationDate { get; set; }. It gives me the date but with 12:00 AM and I do not need the time. Can you help please? Thank you.

1 Answer 1

1

In the response, you get the date as json format. You can create a custom filter to convert json date format to date.

.filter("filterdate", function() {
    var re = /\/Date\(([0-9]*)\)\//;
    return function(x) {
        var m = x.match(re);
        if( m ) return new Date(parseInt(m[1]));
        else return null;
    };
});

add the filter in the html

<td>{{record.CreationDate | filterdate | date:'dd/MM/yyyy'}}</td>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This worked like a charm. Now I know how to use filter!

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.