如何更改datatable中日期列的日期格式? [英] How to change date format for the date column in datatable?
问题描述
它包含两个字段:
DATE
, TIME
两个字段都是 datetime
列
我想迭代数据表并更改日期格式 DATE
列即 dd / MM / yyyy
int i = 0;
string d =;
foreach(dataTable.Rows中的DataRow dr)
{
d = dr [DATE]。ToString();
DateTime date = Convert.ToDateTime(d);
d = date.ToString(dd / MM / yyyy);
dataTable.Rows [i] [DATE] = d;
i ++;
}
我收到以下错误
字符串未被识别为有效的DateTime。
在DATE列中无法存储< 15/02/2015>
。预期类型是DateTime。如何实现?
,你没有告诉我们你创建了你的 dataTable
,但这里是我的想法..
> DATE 和 TIME
列是 DateTime
,那么您需要提供它们 Datetime
值。不是 string
。
在.NET Framework中, DateTime
结构没有任何隐式格式。它只是具有日期和时间值。当您尝试格式化时,您可以获得它的字符串表示。这就是为什么 15/02/2015
将是一个字符串
,而不是一个 DateTime
。
您的 Convert.ToDateTime您将获得
方法可能是因为您的 FormatException
d
值不是的az4se3k1(v = vs.110).aspxrel =nofollow .globalization.cultureinfo.currentculture%28v = vs.110%29.aspxrel =nofollow> CurrentCulture
。您可以使用自定义日期和时间解析,使用 DateTime.ParseExact
或 DateTime.TryParseExact
方法。
string s =15/02/2015;
DateTime dt;
if(DateTime.TryParseExact(s,dd / MM / yyyy,CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dt))
{
控制台.WriteLine(dt);
}
即使你这样做,你为什么要保持 datetime
键入列中的字符串值?这完全没有意义 。即使你这样做,因为 .Rows
返回一个 DataRowCollection
,你可能会得到集合被修改;枚举操作可能无法执行错误,因为您尝试在迭代时修改集合。
我建议您创建另一个 DataTable
为您的字符串值,并将它们添加到 DateTime
值字符串表示形式为 dd / MM / yyyy
格式如;
int i = 0;
string d =;
var stringDataTable = new DataTable();
stringDataTable.Columns.Add(DATE,typeof(String));
stringDataTable.Columns.Add(TIME,typeof(String));
foreach(dataTable.Rows中的DataRow)
{
d =((DateTime)dr [DATE])ToString(dd / MM / yyyy,CultureInfo不变文化);
stringDataTable.Rows [i] [DATE] = d;
i ++;
}
I am filling the datable from database.
It contains two field: DATE
, TIME
Both the fields are datetime
column
I want to iterate through the datatable and change the date format for the DATE
column i.e dd/MM/yyyy
int i = 0;
string d="";
foreach (DataRow dr in dataTable.Rows)
{
d = dr["DATE"].ToString();
DateTime date = Convert.ToDateTime(d);
d = date.ToString("dd/MM/yyyy");
dataTable.Rows[i]["DATE"] = d;
i++;
}
I get the following error
String was not recognized as a valid DateTime.
Couldn't store <15/02/2015>
in DATE Column. Expected type is DateTime. How can I achieve it?
Well, you didn't tell us how you create your dataTable
but here my thought..
If your both DATE
and TIME
columns are DateTime
, then you need to supply them Datetime
values. Not string
.
In .NET Framework, DateTime
structure doesn't have any implicit format. It just have date and time values. You get it's string representation when you try to format it. That's why 15/02/2015
will be a string
in your case, not a DateTime
.
You get FormatException
on your Convert.ToDateTime
method probably because your d
value was not a standard date and time format for your CurrentCulture
. You can use custom date and time parsing with using DateTime.ParseExact
or DateTime.TryParseExact
methods.
string s = "15/02/2015";
DateTime dt;
if(DateTime.TryParseExact(s, "dd/MM/yyyy", CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dt))
{
Console.WriteLine(dt);
}
Even if you do that, why do you wanna keep string
values in a datetime
typed column? That doesn't make sense at all. Even if you do that, since .Rows
returns a DataRowCollection
, you probably will get Collection was modified; enumeration operation may not execute error since you try to modify your collection while you iterating it.
I suggest you to create another DataTable
for your string values and add them to your DateTime
values string representation as dd/MM/yyyy
format like;
int i = 0;
string d = "";
var stringDataTable = new DataTable();
stringDataTable.Columns.Add("DATE", typeof(String));
stringDataTable.Columns.Add("TIME", typeof(String));
foreach(DataRow dr in dataTable.Rows)
{
d = ((DateTime)dr["DATE"]).ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
stringDataTable.Rows[i]["DATE"] = d;
i++;
}
这篇关于如何更改datatable中日期列的日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!