Search code examples
c#kendo-uiteleriktelerik-grid

Telerik Display time in kendo Client Template from integer


I have a Telerik grid and am using an integer to set the Y axis.

Number of seconds, what I want to do is change the label on the chart from

 97 seconds
 132 seconds

as an interger to show time like below

 01:37
 02:12 

by updating the template, this should be fairly straight forward, just cant seem to find the right syntax

lineSeries1.TooltipsAppearance.ClientTemplate = "#= value # seconds";

to be something like

lineSeries1.TooltipsAppearance.ClientTemplate = "#= value #.format('hh:MM:ss') ";

Solution

  • Use this function on your template:

    function formatTime(time) {
        var rootDate = new Date(new Date(1, 1, 1, 0, 0, 0).setSeconds(time));
    
        var minutes = ("0" + rootDate.getMinutes()).slice(-2);
        var seconds = ("0" + rootDate.getSeconds()).slice(-2);
    
        return minutes + ":" + seconds;
    }
    

    And the template as "#: formatTime(value) #"

    Demo.

    • Obs #1: Note that that function is made only for your need, with it you lost the kendo's globalization and your time would always displayed as 00:00;

    • Obs #2: The lib @ezanker proposed is very good tho but it is overkill imo if you only need that what you asked.