且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

字符串未被识别为日期时间格式

更新时间:2022-04-25 22:04:54

首先,我认为您错过了ParseExact方法中格式标准的目的。

format参数定义输入字符串(即order1)的格式。在你的情况下,你说你期待一个格式为dd-MM-yyyy HH:mm的字符串,而实际上你传入的格式是dd / MM / yyyy HH:mmtt作为结果它失败了。



我个人建议使用TryParse而不是ParseExact,以防你收到的字符串不符合你期望的格式。
Firstly, I think you have miss-understood the purpose of the "format" criteria on the ParseExact method.
The format parameter defines how the input string (i.e. order1) should be formatted. In your case you are saying you are expecting a string in the format of "dd-MM-yyyy HH : mm" when in fact you are passing in a string in the format of "dd/MM/yyyy HH:mmtt" as a result it is failing.

Personally I would suggest using TryParse instead of ParseExact in case the strings you are receiving are not in the format you expect.


转换字符串do ==>

string S = String.Format({0:dd-MM-yyyy HH:mm},order1);

DateTime d;

DateTime.TryParse(S,d);

item.orderDateTime = d;

试试这个并告诉我它是否有效..



问候,

Ibrahim Karakira
to convert the string do==>
string S= String.Format("{0:dd-MM-yyyy HH:mm}", order1);
DateTime d;
DateTime.TryParse(S, d);
item.orderDateTime =d;
Try this and Tell me if it works..

Regards,
Ibrahim Karakira


谢谢您的建议,但我还有一个解决方案........ ..



我的实际问题是我有一个字符串如22/07/2013 6:28 PM

i想要将其转换为22-07-1 / 2013 18:28



Datetime.Tryparse()根据要求不能正常工作
Thanks For your suggestions but i got one more solution..........

my actual problem is i have a string like "22/07/2013 6:28PM "
i want to convert it as "22-07-1/2013 18:28"

Datetime.Tryparse() is not working prperly according to the requirement