Format DateTime in Kendo UI Grid using asp.net MVC Wrapper

≯℡__Kan透↙ 提交于 2019-12-02 20:07:48

If you want to display datetime format in kendo grid then do this,

.Format("{0:dd/MM/yyyy}") 

Or

builder.ToString("dd/MM/yyyy");
.Format("{0:" + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern + "}");

There may be some other options in System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat that might work for you if that is not what you want.

The other solutions were close but no cigar... Here's what worked for me:

columns.Bound(c => c.CreatedDate).ClientTemplate("#= kendo.toString(kendo.parseDate(CreatedDate), 'dd/MM/yyyy') #");

Can also use:

columns.Bound(c => c.DateCreate).Format("{0:G}")

As in http://docs.telerik.com/kendo-ui/framework/globalization/dateformatting

Try instead this,this will work.

.ClientTemplate("#= kendo.toString(kendo.parseDate(Date,'dd/MM/yyyy'), '" +  CurrentDateFormat + "') #");

I don't know about Kendo UI but it looks to me like you want to pass a string formatted date rather than a DateTime object.

The /Date(...)/ output looks like a JSON formatted date from .Net.

I would convert the date to a string using somthing like myDateTime.ToString("dd/MM/yyyy"); before passing it to the control.

JebaDaHut

The core issue is documented really well here. Combining the answers there with other stuff I found, here's what I had to do to get it to work on my project.

In the C# code:

.Template("#= kendo.toString(parseDate(" + field.Name + "), 'dd/MM/yyyy') #");

Then, create a javascript function:

function parseDate(d) {
  d = new Date(parseInt(d.replace(/\/Date\((-?\d+)\)\//gi, "$1"), 10));
  return d;
}

It's a bit of a kluge, but works.

Thanks for your answers:

I format a duration in seconds in HH:MM:SS in a Kendo grid column using a ClientTemplate and calling a javascript function:

                .ClientTemplate("#= secToHHMMSS(DurationInSeconds) # ")
                    .Title("Duration")
                    .Width(150);

function secToHHMMSS(s) {
    f = Math.floor;
    g = (n) => ('00' + n).slice(-2);
    return f(s / 3600) + ':' + g(f(s / 60) % 60) + ':' + g(s % 60)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!