[英]How to change date time format in a grid in asp.net mvc
I want to change my date time format from 7/20/2016 10:30:00 AM
to 20 July 2016 10:30 AM
我想将日期时间格式从
7/20/2016 10:30:00 AM
为20 July 2016 10:30 AM
Bellow is the image in where i want to change the format波纹管是我想更改格式的图像
Bellow is my code in controller where i have a events data table and in there columns i have added the event name , occurrence time and recovery time波纹管是我在控制器中的代码,我有一个事件数据表,在那里我添加了事件名称、发生时间和恢复时间
DataTable events = new DataTable();
events.Columns.Add("Event_Name", typeof(string));
events.Columns.Add("Occurrence_Time", typeof(string));
events.Columns.Add("Recovery_Time", typeof(string));
while (reader_events.Read())
{
events.Rows.Add(Convert.ToString(reader_events["Event_Name"]),
Convert.ToString(reader_events["Occurrence_Time"]).ToString(),
Convert.ToString(reader_events["Recovery_Time"]));
}
After that i have passed these events in view data ViewData["events"] = events;之后,我在视图数据中传递了这些事件 ViewData["events"] = events;
Bellow is my razor syntax波纹管是我的剃刀语法
if (ViewData["events"] != null)
{
if (ViewData.Values != null && ViewData.Values.Count() > 0)
{
foreach (System.Data.DataRow dr in (ViewData["events"] as System.Data.DataTable).Rows)
{
<tr>
<td style="border:1px solid black; color:green;font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif">
@dr[0]
</td>
<td style="border:1px solid black; color:green;font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif">
@dr[1]
</td>
<td style="border:1px solid black; color:green;font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif">
@dr[2]
</td>
</tr>
}
}
}
Any help would be highly appreciated任何帮助将不胜感激
you can use the following你可以使用以下
@(string.Format("{0:dd MMMM yyyy hh:mm tt}",dr[1]))
in case dr[1] was string, you need to parse it as datetime then apply formatting like this:如果 dr[1] 是字符串,则需要将其解析为日期时间,然后应用如下格式:
@(string.Format("{0:dd MMMM yyyy hh:mm tt}",DateTime.Parse(dr[1])))
please note that the parse in order to work perfectly you need to have some culture validation请注意,解析为了完美地工作,您需要进行一些文化验证
another solution, is to setup the datetime from the controller it self by using typeof(DateTime)
instead of typeof(string)
另一种解决方案是通过使用
typeof(DateTime)
而不是typeof(string)
来设置它自己的控制器的日期时间
for example:例如:
events.Columns.Add("Occurrence_Time", typeof(DateTime));
events.Columns.Add("Occurrence_Time", typeof(DateTime));
Convert.ToDateTime(reader_events["Occurrence_Time"]),
Convert.ToDateTime(reader_events["Occurrence_Time"]),
string.Format
from the view sidestring.Format
Hope this will help you希望能帮到你
Use DateTime.ParseExact,使用 DateTime.ParseExact,
e.g.: DateTime.ParseExact("12/02/21 10:56:09", "yy/MM/dd HH:mm:ss",
CultureInfo.InvariantCulture
).ToString("MMM. dd, yyyy HH:mm:ss")
Modify the code according to your need.根据需要修改代码。
您可以使用 .ToString 完整日期和时间
yourDate.ToString("dd MMMM yyyy hh:mm tt", CultureInfo.CreateSpecificCulture("en-US"));
在数据表中添加数据并检查期间,将Convert.ToString(reader_events["Recovery_Time"])
更改为reader_events["Recovery_Time"].ToString("dd MMMM yyyy hh:mm tt")
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.