asp.net-mvc 如何在mvc web grid中格式化日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7326687/
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
How to format date in mvc web grid
提问by Crime Master Gogo
I have a web grid and I am not using razor syntex.Rather I am using the .aspx form. the code is below;
我有一个网络网格,但我没有使用 razor syntex。相反,我使用的是 .aspx 表单。代码如下;
<%
var grid = new WebGrid(Model,defaultSort:"PublishDate",rowsPerPage:10);
%>
<%:
grid.GetHtml(
tableStyle: "wGrid",
headerStyle: "wGridHeader",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("Title", canSort: false),
grid.Column("PublishDate", "Published on"),
grid.Column("CategoryName", "Category"),
grid.Column(format: (item) => Html.ActionLink("Details", "Browse", new { id = item.Title }))
)
)
%>
Now I want to format the 'PublishDate' column to something like 'dd-MMM-yyyy'. Any idea how to do this?
现在我想将“PublishDate”列的格式设置为“dd-MMM-yyyy”。知道如何做到这一点吗?
回答by Darin Dimitrov
grid.Column(
"PublishDate",
"Published on",
format: (item) => string.Format("{0:dd-MMM-yyyy}", item.PublishDate)
)
回答by Mayank
If DateTime Property is defined as (can contain null) :
如果 DateTime 属性定义为(可以包含 null):
public DateTime? WorkedDate { get; set; }
Use this format:
使用这种格式:
grid.Column("WorkedDate", "Last Worked On",
format: (item) => item.WorkedDate != null
? item.WorkedDate.ToString("MM/dd/yy") : "", canSort: true)
Otherwise if it is defined as below (can't be null), it will have either actual date or .MinDate as the default.
否则,如果它定义如下(不能为空),它将以实际日期或 .MinDate 作为默认值。
public DateTime WorkedDate { get; set; }
Use format:
使用格式:
grid.Column("WorkedDate", "Last Worked On",
format: (item) => item.WorkedDate != DateTime.MinValue ?
item.WorkedDate.ToString("MM/dd/yy") : "", canSort: true)
回答by Avery West
This works for me, and allows for Null values
这对我有用,并允许空值
grid.Column("End_Date",format: item => ((item.End_Date == null) ? "" : item.End_Date.ToString("MM/dd/yyyy"))),
回答by Sanjana V
This worked for me:
这对我有用:
grid.Column("Date_Of_Birth", "Date Of Birth", format: item => ((item.Date_Of_Birth == null) ? "" : item.Date_Of_Birth.ToString("MM/dd/yyyy")))

