简体   繁体   中英

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

I want to build a Kendo UI Grid with format date dd//MM/yyyy. However, all questions that I found about this, it were resolved with code Format("{0:d}"); . So, I have tried like a code below:

GridBoundColumnBuilder<TModel> builder = par.Bound(field.Name);

        switch (field.Type.Type)
        {
            case CType.Boolean:
                builder = builder.ClientTemplate(string.Format("<input type='checkbox' #= {0} ? checked='checked' : '' # disabled='disabled' ></input>", field.Name));
                break;
            case CType.Datetime:
                builder = builder.Format("{0:d}");
                break;
            case CType.Decimal:
            case CType.Double:
                builder = builder.Format("{0:0.00}");
                break;
        }

Another formats is works fine, just DateTime do not works.

I had this result for Datetime = /Date(1377020142000)/

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 + "}");

System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat中可能还有其他一些选项可能对您有用,如果这不是您想要的。

其他解决方案很接近,但没有雪茄......这对我有用:

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

试试这个,这将有效。

.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.

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) } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM