字符串未被识别为有效日期时间,日期应以24/12/2012 12:00:00格式显示 [英] string was not recognise as valid date time,date should be display in format 24/12/2012 12:00:00

查看:153
本文介绍了字符串未被识别为有效日期时间,日期应以24/12/2012 12:00:00格式显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将datarow从一个表复制到另一个数据表列structrure不相同因此我使用

DataTable dt = new DataTable();

i am copying datarow from one table to another datatable column structrure are not same hence i use
DataTable dt = new DataTable();

dt.Columns.Add("START DATE", typeof(System.DateTime));
 dt.Columns.Add("END DATE",typeof(System.DateTime));





DataRow row = dt.NewRow();



//从旧表中复制行

foreach(WorksheetInfo.Rows中的DataRow博士)

{

row = dt.NewRow();

row [0] = dr [StartDate]。ToString();

row [1] = dr [EndDate]。ToString();

dt.Rows.Add(row);

}

dt.AcceptChanges();



i希望日期应以格式显示 24/12/2012 12:00:00 其要求项目无法更改。



但现在我在datable中显示的当前日期仅为12/24/2012



我尝试一切例子



1)string dtenddate = dr [EndDate]。ToString();

DateTime dtendate = Convert.ToDateTime(dtenddate);

row [1] = dtendate;



2)string dtdate = String.Format({0 d / M / yyyy HH:mm:ss},转换.ToDateTime(DR [ 结束日期]));

row [1] = dtdate;

任何更多

最后一个选项我尝试进行数组拆分事件如果这里显示错误



DataRow row = dt.NewRow();

//copy row from old table
foreach (DataRow dr in WorksheetInfo.Rows)
{
row = dt.NewRow();
row[0] = dr["StartDate"].ToString();
row[1] = dr["EndDate"].ToString();
dt.Rows.Add(row);
}
dt.AcceptChanges();

i want date should be display in format 24/12/2012 12:00:00 its requirement of project can not change.

but now my current date showing in datable is 12/24/2012 only

I try everything example

1) string dtenddate = dr["EndDate"].ToString();
DateTime dtendate = Convert.ToDateTime(dtenddate);
row[1] = dtendate;

2)string dtdate = String.Format("{0 d/M/yyyy HH:mm:ss}",Convert.ToDateTime(dr["EndDate"]));
row[1] = dtdate;
any many more
last option i try for array split event if here it show error

DateTime dtstart = Convert.ToDateTime(dr["EndDate"]);                       string[] strDateTime = dtstart.ToString("dd/MM/yyyy").Split('/');

                       // displaying values in textboxes

                     string  txtDate = strDateTime[0];
                     string  txtMonth = strDateTime[1];
                      string txtYear = strDateTime[2];





但显示错误,因为字符串无法识别为有效日期时间,任何人都知道如何获得我的要求



but it showing error as string was not recognise as valid date time,anyone have idea how to get my requirement

推荐答案

从不使用Convert.ToDateTime,而是使用DateTime.TryParseExact。

接下来,您可以通过提供格式字符串来修改DateTime对象的字符串表示的格式。 tostring方法:

Never use Convert.ToDateTime, but rather DateTime.TryParseExact.
Next you can modify the format of a string represenation of a DateTime object by providing the format string in the tostring method:
DateTime mydts = DateTime.Now;
string mystringdts = mydts.ToString("dd/MM/yyyy HH:mm:ss");





在哪里 [ ^ ]

在其中 [ ^ ]



希望这会有所帮助。 />


PS:不要将日期时间视为您看到的东西(如2005年4月26日的2005年4月26日),它是一个能够以多种方式表示的对象。因此,除非您指定它需要的格式,否则它将采用与机器相关的形式。 (文化设置)



see here[^]
and here[^]

hope this helps.

PS: Do not see a datetime as something you see (like 26/04/2005 for April 26th 2005) it is an object capable of being represented in numerous ways. Therefore, unless you SPECIFY the format it needs to be, it will take the form in a very machine dependent way. (cultural settings)


当你说数据表中的日期显示格式错误时 - 你怎么看?



如上所述,您如何看待它完全取决于您使用它来查看它。如果您使用某种工具查看数据表,那么该工具将决定如何显示日期 - 它始终在内部存储相同 - 它是日期,而不是日期的字符串表示。



在数据库中,日期存储为二进制表示,显示它的行为需要一些翻译 - 如果没有指定,您可能会使用机器当前的文化来确定预期的格式。



如果您的日期以您不想要的格式显示,假设您使用的任何工具显示它是使用系统值,您将必须修改运行它的操作系统。



例如美国机器的默认日期格式可能是MM / DD / YYYY,但英国是DD / MM / YYYY
When you say the date shows in your datatable in the wrong format - how are you looking at it?

As mentioned, how you see it is entirely dependent on what you are using to look at it. If you are using some tool to look at the data table then that tool will decide how to display the date - it is always stored the same internally - it is a date, not a string representation of a date.

In the database the date is stored as a binary representation, and the act of displaying it requires some translation - which if not specified by you will probably use your machine's current culture to determine the expected format.

If your date is being displayed in a format you don't want then, assuming whatever tool you are using to display it is using a system value, you will have to modify the OS on which it is running.

e.g. the default date format for US machines is probably MM/DD/YYYY but UK is DD/MM/YYYY


这篇关于字符串未被识别为有效日期时间,日期应以24/12/2012 12:00:00格式显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆