将DataRow中的DateTime转换为格式化的日期字符串 [英] Convert DateTime in DataRow to a formatted date string
问题描述
foreach(dt.Rows中的DataRow行)
{
row [StartOn] = Convert.ToDateTime(row [StartOn]。ToString())。ToString(MMM dd)ToString ();
}
如果我运行以下代码,我将获得Aug 09
Convert.ToDateTime(row [StartOn]。ToString())ToString(MMM dd)ToString()
如果我看到这个更改后的行[StartOn]是什么,它包含8 / 9/2016 12:00:00 AM
我无法将DataRow格式化为MMM dd格式
StartOn显然是一个DateTime类型。 DateTime类型没有格式。它们是指定年,月,日期和时间的对象(除其他外)。您在转化中所做的所有工作都是剥离时间,以便新的日期时间的上午12:00。
I'm hoping that there is something I am not seeing clearly, but to simplify, I have the below code
foreach (DataRow row in dt.Rows)
{
row["StartOn"] = Convert.ToDateTime(row["StartOn"].ToString()).ToString("MMM dd").ToString();
}
If I run the below code I get "Aug 09"
Convert.ToDateTime(row["StartOn"].ToString()).ToString("MMM dd").ToString();
If I look to see what is in row["StartOn"] after this change it contains "8/9/2016 12:00:00 AM"
I'm unable to format my DataRow to an "MMM dd" format
StartOn is apparently a DateTime type. DateTime types do NOT have a format. They are an object that specifies year, month, date, and time (among other things). All you are doing in your conversions is stripping out the time so that the new datetime has a time of 12:00 am.
这篇关于将DataRow中的DateTime转换为格式化的日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!