Ibanez1700
Ibanez1700

Reputation: 89

Formatting Sql Date Value to Date only using angularjs

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.

Upvotes: 0

Views: 2456

Answers (1)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41445

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>

Upvotes: 1

Related Questions