1

My ASP MVC controller returns the results of the database in Json and the date field in epoch:

public ActionResult List(int id)
{

    IEnumerable<object> query = (from a1 in db.Persons
                                 where a1.personID == id
                                 select new
                                 {
                                        personID = a1.personID,
                                        date = a1.Date,
                                        Firstname = a1.Firstname,
                                        Lastname = a1.Lastname,

                                 }).ToList();

    return Json(query);
}

The Jquery result:

{firstName:'James',lastName:'Smith',date:"/Date(1447110000000)/"},
{firstName:'Susan',lastName:'Smith',date:"/Date(1447110000000)/"},
....

See more at: http://jsfiddle.net/EZUEF/749/

I tried to do it in the controller with:

date = a1.Date..ToString("dd/MM/yyyy")

It does not work. What is the best solutions to convert the Epoch date to dd/mm/yyyy? In KnockoutJS or in the controller?

Citizen SP
  • 1,411
  • 7
  • 36
  • 68
  • Possible duplicate of [How do you convert epoch time in C#?](http://stackoverflow.com/questions/2883576/how-do-you-convert-epoch-time-in-c) – Luke Nov 30 '15 at 09:27
  • Sorry, I just realised it's not being posted in, it's being returned.. whoops. However, I would do the conversion on the server side. – Luke Nov 30 '15 at 09:32

1 Answers1

2

You can use momentjs.

data-bind="text: moment(date).format('DD/MM/YYYY')"

jsfiddle link

tmg
  • 19,895
  • 5
  • 72
  • 76