[英]String was not recognized as a valid DateTime when parse exact
I get the following exception when converting to DateTime: 转换为DateTime时出现以下异常:
String was not recognized as a valid DateTime.
lbl_RequestDate.Text = "13/2/2013";
CultureInfo provider = CultureInfo.CurrentCulture;
string[] format = provider.DateTimeFormat.GetAllDateTimePatterns();
Follow.RequestDate = DateTime.ParseExact(lbl_RequestDate.Text, format, provider, DateTimeStyles.None);
You can use thje format d/M/yyyy
, Notice the single M
used for the month. 您可以使用格式
d/M/yyyy
,注意本月使用的单个M
Follow.RequestDate = DateTime.ParseExact(lbl_RequestDate.Text, "d/M/yyyy", provider, DateTimeStyles.None);
The method: provider.DateTimeFormat.GetAllDateTimePatterns()
returns almost 155 formats, but none of them (from your current culture seems to) supports format d/M/yyyy
that is why you are getting the exception. 方法:
provider.DateTimeFormat.GetAllDateTimePatterns()
返回近155种格式,但没有一种(来自您当前的文化)支持格式d/M/yyyy
,这就是您获得异常的原因。 If your date has Month as 13/02/2013
then the formats returned by the method would work since the closest format is dd/MM/yyyy
in the formats array. 如果您的日期为月
13/02/2013
日为13/02/2013
月13/02/2013
日,那么该方法返回的格式将起作用,因为格式数组中最接近的格式为dd/MM/yyyy
。
Maybe this will help : 也许这会有所帮助:
DateTime.ParseExact("13/2/2013","d/M/yyyy",CultureInfo.GetCultureInfo("en-US"), DateTimeStyles.None );
notice : 注意 :
d is for Day (01 is also acceptable)
M is for Month (11 is also acceptable)
试试这样:
Follow.RequestDate = DateTime.ParseExact(lbl_RequestDate.Text, "d/M/yyyy", CultureInfo.InvariantCulture);
DateTimeFormatInfo.GetAllDateTimePatterns()
method returns on my machine ( tr-TR
Culture) 29 format but none of these support d/M/yyyy
date format, that's why you are getting FormatException
. DateTimeFormatInfo.GetAllDateTimePatterns()
方法在我的机器上返回( tr-TR
Culture)29格式,但这些都不支持d/M/yyyy
日期格式,这就是你获得FormatException
的原因。
But in my culture DateSeparator
is .
但在我的文化中,
DateSeparator
是.
so I can't exactly solve this problem using CultureInfo.CurrentCulture
but when I use Egypt cultureinfo (it's wrote on your profile) CultureInfo.GetCultureInfo("ar-EG")
this code works without any error; 所以我无法使用
CultureInfo.CurrentCulture
完全解决这个问题但是当我使用埃及 cultureinfo(它是在你的个人资料上写的) CultureInfo.GetCultureInfo("ar-EG")
这段代码没有任何错误;
CultureInfo provider = CultureInfo.GetCultureInfo("ar-EG");
string[] format = provider.DateTimeFormat.GetAllDateTimePatterns();
DateTime d = DateTime.ParseExact("13/02/2013", format, provider, DateTimeStyles.None);
Unfortunatly your your all datetime pattern doesn't support d/M/yyyy
format. 不幸的是,您的所有日期时间模式都不支持
d/M/yyyy
格式。
Unfortunatly , changing this string to
13/02/2013
doesn't solve this problem because as I said on before, my all formats (in
tr-TR
Culture) doesn't support
dd/MM/yyyy
format either.
不幸的是 ,将此字符串更改为
13/02/2013
并不能解决这个问题,因为正如我之前所说,我的所有格式(在
tr-TR
文化中)也不支持
dd/MM/yyyy
格式。
My humble advice is here, list all your datetime patterns and check manually if your string is recognized format with this datetime pattern like; 我的谦卑建议在这里,列出你所有的日期时间模式,并手动检查你的字符串是否是这个日期时间模式的识别格式,如;
string[] format = provider.DateTimeFormat.GetAllDateTimePatterns();
foreach (var f in format)
{
///
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.