C# 使用 asp.net MVC Wrapper 在 Kendo UI Grid 中格式化 DateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18718996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:00:58  来源:igfitidea点击:

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

c#asp.net-mvc-4datetimekendo-gridkendo-asp.net-mvc

提问by leodeoliveira

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:

我想构建一个格式为日期 dd//MM/yyyy 的 Kendo UI 网格。但是,我发现的所有有关此问题的问题都已通过代码Format("{0:d}"); 解决。. 所以,我尝试过像下面的代码:

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.

另一种格式工作正常,只是 DateTime 不起作用。

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

我对 Datetime = /Date(1377020142000)/ 有这个结果

采纳答案by Jaimin

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

如果要在剑道网格中显示日期时间格式,请执行以下操作,

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

Or

或者

builder.ToString("dd/MM/yyyy");

回答by Void

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.

我不了解 Kendo UI,但在我看来,您想传递字符串格式的日期而不是 DateTime 对象。

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

/Date(...)/输出看起来像一个JSON从.net格式的日期。

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

在将日期myDateTime.ToString("dd/MM/yyyy");传递给控件之前,我会使用类似的方法将日期转换为字符串。

回答by Bryan Hobbs

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

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

回答by Milan

Try instead this,this will work.

试试这个,这会起作用。

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

回答by 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:

在 C# 代码中:

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

Then, create a javascript function:

然后,创建一个 javascript 函数:

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

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

这有点笨拙,但有效。

回答by user1477388

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') #");

回答by piris

回答by Ephie

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 并调用 javascript 函数在 Kendo 网格列中的 HH:MM:SS 中以秒为单位格式化持续时间:

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