Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format date in Kendo Template

I'm trying to format my DateTime object in my Kendo ListView Template but the suggested kendo.toString method doesn't seem to work for me.

I've cut out a lot of code that doesn't relate to my problem to make it a little more simple to understand.

I have a Kendo DataSource that looks like the following:

contactDataSource: new kendo.data.DataSource({
    transport: {
        read: {
            url: "/baa/contact/getcontacts",
            dataType: "json",
            type: "GET"
        }
     },
     schema: {
        model: {
            id: "Id",
            fields: {
                Id: { type: "number", editable: false, nullable: true },
                CompanyName: { type: "string" },
                ContactName: { type: "string" },
                ContactPhone: { type: "string" },
                ContactEmail: { type: "string" },
                ImageUrl: { type: "string" },
                Website: { type: "string" },
                RecentBaas: [
                    {
                        Name: { type: "string" },
                        StatusDisplay: { type: "string" },
                        Status: { type: "number" },
                        LastModDate: { type: "date" }
                    }
                ]
            }
        }
    }
})

And then I have a template on my view that looks like the following:

<script type="text/x-kendo-templ" id="contactTemplate">
    <div class="row">
        <div class="col-md-12">
            # for (var i = 0; i < RecentBaas.length; i++) { #
            # if (RecentBaas[i].Status == 1) { #
                <div class="alert alert-success" role="alert">
                    <p>#=kendo.toString(RecentBaas[i].LastModDate, "F")#</p>
                </div>
            # } #
            # } #
        </div>
    </div>
</script>

I'm not getting any errors in my console when I load this page, but the date is not formatted at all. It still just shows as /Date(1461203814927)/ for example.

I've read the Kendo Documentation on how to use the toString function to format DateTime objects and as far as I can tell I'm doing everything right. But maybe I'm still missing something?

like image 286
Quiver Avatar asked Aug 03 '16 16:08

Quiver


People also ask

What is date formatting in Kendo UI for jQuery?

New to Kendo UI for jQuery ? Download free 30-day trial The purpose of date formatting is to convert the Date object to a human readable string by using the culture-specific settings. The kendo.format and kendo.toString methods support standard and custom date formats.

What is Kendo UI templates?

Kendo UI Templates trade convenient syntax sugar for improved performance, which distinguishes it from other templating JavaScript libraries. The Templates is part of Kendo UI for jQuery, a professional grade UI library with 110+ components for building modern and feature-rich applications. To try it out sign up for a free 30-day trial.

What are hash templates in kendo?

The Kendo UI Templates use a simple templating syntax called hash templates. With this syntax, the # (hash) sign is used to mark areas in a template that should be replaced by data when the template is executed. The # character is also used to signify the beginning and end of custom JavaScript code inside the template.

What is the purpose of date formatting?

Download free 30-day trial The purpose of date formatting is to convert the Date object to a human readable string by using the culture-specific settings. The kendo.format and kendo.toString methods support standard and custom date formats.


1 Answers

Please try with the below code snippet.

<script type="text/x-kendo-templ" id="contactTemplate">
    <div class="row">
        <div class="col-md-12">
            # for (var i = 0; i < RecentBaas.length; i++) { #
            # if (RecentBaas[i].Status == 1) { #
                <div class="alert alert-success" role="alert"> <p>#=kendo.toString(kendo.parseDate(RecentBaas[i].LastModDate), 'yyyy-MM-dd')#</p>
                </div>
            # } #
            # } #
        </div>
    </div>
</script>

Let me know if its not working

like image 111
Jayesh Goyani Avatar answered Oct 10 '22 02:10

Jayesh Goyani