C# DateTime.TryParseExact() 拒绝有效格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11999912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 20:01:38  来源:igfitidea点击:

DateTime.TryParseExact() rejecting valid formats

c#.netcultureinfo

提问by see sharper

I'm parsing a DateTime value in an ASP.NET WebFormspage 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 FormatProviderin the above, butthe same problem occurs when I provide a CultureInfoobject as (CultureInfo provider = new CultureInfo("en-US"))for this parameter.

注意我null FormatProvider在上面给出了 a ,但是当我为此参数提供一个CultureInfo对象 时会出现同样的问题(CultureInfo provider = new CultureInfo("en-US"))

What am I missing?

我错过了什么?

采纳答案by Adil Mammadov

Try:

尝试:

 DateTime.TryParseExact(txtStartDate.Text, formats, 
        System.Globalization.CultureInfo.InvariantCulture,
        System.Globalization.DateTimeStyles.None, out startDate)

回答by Jidheesh Rajan

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.

这应该对你有用。

回答by Amnesh Goel

Here you can check for couple of things.

在这里,您可以检查几件事。

  1. Date formats you are using correctly. You can provide more than one format for DateTime.TryParseExact. Check the complete list of formats, available here.
  2. CultureInfo.InvariantCulturewhich is more likely add problem. So instead of passing a NULLvalue orsetting it to CultureInfo provider = new CultureInfo("en-US"), you may write it like. .

    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
    }
    
  1. 您正确使用的日期格式。您可以为DateTime.TryParseExact. 查看完整的格式列表,可在此处获取
  2. CultureInfo.InvariantCulture这更有可能增加问题。因此,您可以这样写,而不是传递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
    }
    

回答by islam elgaidi

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/en-us/library/ms131044(v=vs.110).aspx

回答by Waleed A.K.

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");