0

I have ASP.NET MVC project and I am trying to format date (dd/MM/yyyy) in my cshtml UI but its returning null.

Below is my cshtml code:

    <tr ng-repeat="item in items">
                        <td>{{item.Id}}</td>
                        <td>{{item.Name}}</td>
                        <td>{{item.Manufacturer}}</td>
                        <td>{{item.BatchNo}}</td>
                        <td width="50px">{{item.ExpiryDate| date: 'dd/MM/yyyy'}}</td>
    </tr>

I tried two ways:

1) formatting in .NET cshtml itself but nothings happening:

<td width="50px">{{item.ExpiryDate| date: 'dd/MM/yyyy'}}</td>

2) I tried using filter but its returning null:

medApp.filter('cmdate', [
    '$filter', function ($filter) {
        return function (input) {
            return $filter('date')(new Date(input), 'dd/MM/yyyy');
        };
    }
]);

code in cshtml:

<td width="50px">{{item.ExpiryDate| cmdate: 'dd/MM/yyyy'}}</td>

Any clue about the issue?

Thanks

12
  • 2
    show the item.ExpiryDate value Commented Dec 6, 2017 at 10:47
  • Is ExpiryDate a date object? Commented Dec 6, 2017 at 10:48
  • @SachilaRanawaka ... Its null when I use the custom filter. If I use {{item.ExpiryDate| date:'dd/MM/yyyy'}} then its /Date(1513881000000)/ I am getting the values from SQL Server and its coming fine. Commented Dec 6, 2017 at 10:51
  • @Liam ...Its a field in product object. Commented Dec 6, 2017 at 10:52
  • 3
    From your comments, you have this issue Commented Dec 6, 2017 at 10:57

1 Answer 1

0

your date passing as json format. create the custom filter like this.

app.filter("cmdate", 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;
    };
});

Now call it like this

<td width="50px">{{item.ExpiryDate| cmdate | date: 'dd/MM/yyyy'}}</td>
Sign up to request clarification or add additional context in comments.

2 Comments

It would be better to fix the serialization at source so that all dates and times are serialized according to ISO8601. So much easier to deal with.
I tried using this but its the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.