[英]DateTime.TryParseExact() rejecting valid formats
I'm parsing a DateTime value in an ASP.NET WebForms
page and the date string keeps getting rejected by the DateTime.TryParseExact()
method even though it clearly matches one of the supplied format strings. 我在
ASP.NET WebForms
页面中解析DateTime值,并且日期字符串不断被DateTime.TryParseExact()
方法拒绝,即使它显然与提供的格式字符串之一匹配也是如此。
It seems to fail on my development machine at home but work on the production server, so I am thinking of local date settings being involved, but this error occurs even when I supply an IFormatProvider (CultureInfo)
object as a parameter 它似乎在我的开发机器上无法正常运行,但在生产服务器上工作,因此我考虑了本地日期设置,但是即使我提供
IFormatProvider (CultureInfo)
对象作为参数,也会发生此错误。
Here's the code: 这是代码:
DateTime startDate;
string[] formats = { "dd/MM/yyyy", "dd/M/yyyy", "d/M/yyyy", "d/MM/yyyy",
"dd/MM/yy", "dd/M/yy", "d/M/yy", "d/MM/yy"};
var errStart = row.FindControl("errStartDate"); //my date format error message
if (!DateTime.TryParseExact(txtStartDate.Text, formats, null, DateTimeStyles.None, out startDate))
{
errStart.Visible = true; //we get here even with a string like "20/08/2012"
return false;
}
else
{
errStart.Visible = false;
}
Note I'm giving a null FormatProvider
in the above, but the same problem occurs when I provide a CultureInfo
object as (CultureInfo provider = new CultureInfo("en-US"))
for this parameter. 注意我在上面提供了一个
null FormatProvider
, 但是当我为此参数提供CultureInfo
对象作为(CultureInfo provider = new CultureInfo("en-US"))
,也会发生相同的问题。
What am I missing? 我想念什么?
Try: 尝试:
DateTime.TryParseExact(txtStartDate.Text, formats,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out startDate)
Here you can check for couple of things. 在这里您可以检查几件事情。
DateTime.TryParseExact
. DateTime.TryParseExact
提供多种格式。 Check the complete list of formats, available here . CultureInfo.InvariantCulture
which is more likely add problem. CultureInfo.InvariantCulture
更有可能添加问题。 So instead of passing a NULL
value or setting it to CultureInfo provider = new CultureInfo("en-US")
, you may write it like. 因此,您可以像这样编写它,而不是传递
NULL
值或将其设置为CultureInfo provider = new CultureInfo("en-US")
。 . 。
if (!DateTime.TryParseExact(txtStartDate.Text, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out startDate)) { //your condition fail code goes here return false; } else { //success code }
This is the Simple method, Use ParseExact 这是简单的方法,使用ParseExact
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime result;
String dateString = "Sun 08 Jun 2013 8:30 AM -06:00";
String format = "ddd dd MMM yyyy h:mm tt zzz";
result = DateTime.ParseExact(dateString, format, provider);
This should work for you. 这应该为您工作。
Try C# 7.0 试试C#7.0
var Dob= DateTime.TryParseExact(s: YourDateString,format: "yyyyMMdd",provider: null,style: 0,out var dt)
? dt : DateTime.Parse("1800-01-01");
string DemoLimit = "02/28/2018";
string pattern = "MM/dd/yyyy";
CultureInfo enUS = new CultureInfo("en-US");
DateTime.TryParseExact(DemoLimit, pattern, enUS,
DateTimeStyles.AdjustToUniversal, out datelimit);
For more https://msdn.microsoft.com/en-us/library/ms131044(v=vs.110).aspx 有关更多https://msdn.microsoft.com/zh-cn/library/ms131044(v=vs.110).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.