字符串未被识别为日期时间格式 [英] String was not recognised as datetime format

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

问题描述







我的字符串格式为22/07/2013 6:28 PM。我想以dd-MM-yyyy HH:mm的格式转换它。



6:28 PM应该是18:28。



i使用的代码如

Hi,


MY string is in the format "22/07/2013 6:28PM". i want to convert this in the format "dd-MM-yyyy HH : mm"

that 6:28PM Should come as 18:28.

i have used the code like

item.orderDateTime = DateTime.ParseExact(order1, format, CultureInfo.InvariantCulture);







item.orderdatetime的类型为DateTime

和order1是类型字符串包含值22/07/2013 6:28 PM

格式为dd-MM-yyyy HH:mm



并且它给出了错误字符串未被识别为日期时间格式



没有得到wat来做所需的建议...... ..........


in this

item.orderdatetime is of type DateTime
and order1 is of type string contains the value "22/07/2013 6:28PM"
format is "dd-MM-yyyy HH : mm"

and it is giving an error "String was not recognised as datetime format"

Not getting wat to do suggestions needed .............

推荐答案

首先,我认为您错过了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


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

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