Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to format a UNIX timestamp into a human date in Kendo UI Grid?

Well there seems to be a multitude of similar questions, but none that I can find answering this specific question.. so here goes..

Have a working Kendo UI grid. My datasource is returning a timestamp - here's the JSON response coming back to the code:

enter image description here

You'll notice that the next line is also a date.. returned by MySQL as a standard DateTime format - which I would be happy to use directly. But I've converted the date to a timestamp which I thought would be more universal. (??)

Now I need to do two things - format the timestamp into a readable date and edit the date so it can be saved back to the datasource. But let's tackle formatting first.

My code to display the column currently looks like this:

    {   title: "Trial<br>Date", 
    field: "customer_master_converted_to_customer_date",
    format: "{0:d/M/yyyy}",
    attributes: {
        style: "text-align: center; font-size: 14px;"
    },
    filterable: true,
    headerAttributes: {
        style: "font-weight: bold; font-size: 14px;"
    }
},

Although I've tried..

    toString(customer_master_converted_to_customer_date, "MM/dd/yyyy")

.. and several variations of that - in terms of format string. And yes, I've tried entering:

    type: "date",

No matter what I do, I only get the timestamp.

enter image description here

Anyone?

like image 977
Lee Fuller Avatar asked Aug 02 '13 23:08

Lee Fuller


1 Answers

You need to convert the timestamp to a JavaScript date first. Here is a sample implementation:

$("#grid").kendoGrid({
  dataSource: {
    data: [
      { date: 1371848019 }
    ],
    schema: {
      model: {
        fields: {
          date: {
            type: "date",
            parse: function(value) {
              return new Date(value * 1000);
            }
          }
        }
      }
    }
  }
});

Here is it live: http://jsbin.com/utonil/1/edit

like image 178
Atanas Korchev Avatar answered Sep 26 '22 16:09

Atanas Korchev