Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DateTime.TryParse仅在yyyy失败_C#_Datetime_Cultureinfo_Date Parsing - Fatal编程技术网

C# DateTime.TryParse仅在yyyy失败

C# DateTime.TryParse仅在yyyy失败,c#,datetime,cultureinfo,date-parsing,C#,Datetime,Cultureinfo,Date Parsing,我有一个过滤器字段,允许按部分日期或完整日期进行搜索,这些日期可以以不同的格式输入——也就是说,用户希望以何种方式输入,因此TryParseExact看起来不是最好的选择 问题在于以下代码: DateTime.TryParse(fromDate.Text, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateFromValue) 解析除yyyy字符串以外的所有内容。例如,“05/1980”和“1980/05”被识别为{1/05/

我有一个过滤器字段,允许按部分日期或完整日期进行搜索,这些日期可以以不同的格式输入——也就是说,用户希望以何种方式输入,因此TryParseExact看起来不是最好的选择

问题在于以下代码:

DateTime.TryParse(fromDate.Text, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateFromValue)
解析除yyyy字符串以外的所有内容。例如,“05/1980”和“1980/05”被识别为{1/05/1980 12:00:00 AM},但“1980”失败

我怀疑解析器无法区分(比如)yyyy和ffff,因为它只能得到一个4位序列,但我仍然需要让它工作。有没有比这更优雅的方法

if(!DateTime.TryParse("1980", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
    DateTime.TryParseExact("1980","yyyy",CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
还是我完全错了


提前谢谢你

如果需要部分匹配,最简单的方法是将日期视为字符串,只使用字符串匹配而不依赖于日期格式。

如果需要部分匹配,最简单的方法是将日期视为字符串,只使用字符串匹配而不依赖于日期格式。

我认为不能使用DateTime。使用只有一年,因为月和日是不明确的。改为使用DateTime.ParseExact或DateTime.TryParseExact与特定日期格式

如果你的情况在我看来是错误的,你如何将这两种情况结合起来

if(!DateTime.TryParse("1980", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
DateTime.TryParseExact("1980","yyyy",CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
应该是:

if(DateTime.TryParse("1980", CultureInfo.InvariantCulture, DateTimeStyles.None, out date) ||
DateTime.TryParseExact("1980","yyyy",CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
// If the first parse success, use that. Otherwise, try to parse the year only with the parseexact.
// Notice the || and the first condition needs to be positive. 

我认为您不能使用DateTime.Parse,只能使用year,因为月份和日期不明确。改为使用DateTime.ParseExact或DateTime.TryParseExact与特定日期格式

如果你的情况在我看来是错误的,你如何将这两种情况结合起来

if(!DateTime.TryParse("1980", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
DateTime.TryParseExact("1980","yyyy",CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
应该是:

if(DateTime.TryParse("1980", CultureInfo.InvariantCulture, DateTimeStyles.None, out date) ||
DateTime.TryParseExact("1980","yyyy",CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
// If the first parse success, use that. Otherwise, try to parse the year only with the parseexact.
// Notice the || and the first condition needs to be positive. 

我一直在考虑这个问题,但TryParse主要有效,因此尝试使其完全有效看起来非常诱人:)我一直在考虑,但TryParse主要有效,因此尝试使其完全有效看起来非常诱人:)好的观点-我应该在复制时更加小心!谢谢你指出:)在这个特殊的情况下,我会完全同意TryParse只是猜测丢失的日期和月份(例如默认情况下1月1日)-如果它能做到的话:(好的观点-我应该在复制时更加小心!谢谢你指出:)在这种特殊情况下,我完全可以让TryParse猜测缺失的日期和月份(例如默认情况下为1月1日)-如果它能够做到这一点:(