c# - 使用 DateTime.TryParse 检查字符串是否为有效日期

标签 c# .net

我正在使用 DateTime.TryParse() 函数来检查特定字符串是否是有效的日期时间,不依赖于任何文化。
令我惊讶的是,该函数对偶数字符串(如“1-1”、“1/1”等)返回 true

我该如何解决这个问题?

更新:

这是否意味着,如果我想检查一个特定的字符串是否是有效的日期时间,我需要大量的格式??我相信会有不同的组合。
即使有很多日期分隔符(“.”、“/”、“-”等)取决于文化,我也很难定义一个格式数组来检查 .
基本上,我想检查一个特定的字符串是否以任何顺序包含至少一天(1 到 31 或 01 到 31)、月份(1 到 12 或 01 到 12)和年份(yyyy 或 yy),以及任何日期分隔符,解决办法是什么?
因此,如果该值包含时间的任何部分,它也应该返回 true。 我无法定义格式数组。

最佳答案

如果您希望您的日期符合一种或多种特定格式,请使用 DateTime.TryParseExact否则这是 DateTime.TryParse

的默认行为

DateTime.TryParse

This method tries to ignore unrecognized data, if possible, and fills in missing month, day, and year information with the current date. If s contains only a date and no time, this method assumes the time is 12:00 midnight. If s includes a date component with a two-digit year, it is converted to a year in the current culture's current calendar based on the value of the Calendar.TwoDigitYearMax property. Any leading, inner, or trailing white space character in s is ignored.

如果您想针对多种格式进行确认,请查看 DateTime.TryParseExact Method (String, String[], IFormatProvider, DateTimeStyles, DateTime)重载。来自同一链接的示例:

string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", 
                   "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", 
                   "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", 
                   "M/d/yyyy h:mm", "M/d/yyyy h:mm", 
                   "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm"};
string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM", 
                        "5/1/2009 6:32:00", "05/01/2009 06:32", 
                        "05/01/2009 06:32:00 PM", "05/01/2009 06:32:00"}; 
DateTime dateValue;

foreach (string dateString in dateStrings)
{
   if (DateTime.TryParseExact(dateString, formats, 
                              new CultureInfo("en-US"), 
                              DateTimeStyles.None, 
                              out dateValue))
      Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
   else
      Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
}
// The example displays the following output: 
//       Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM. 
//       Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM. 
//       Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM. 
//       Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM. 
//       Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM. 
//       Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM.

关于c# - 使用 DateTime.TryParse 检查字符串是否为有效日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16075159/

相关文章:

c# - 由于 Unresolved 依赖关系,无法使用 Winforms 控件

c# - if条件内的sql select语句

c# - 在 C# 中,如何检查 TCP 端口是否可用?

c# - IIS认证中的 "realm"是什么,它和SSL证书参数有什么关系?

c# - 使用httprequest下载文件

c# - htmlagilitypack - 删除脚本和样式?

javascript - Ajax 更新面板随机错误 'PRM_MissingPanel'

c# - 从 Entity Framework 6 + MySQL 的代码设置数据库创建/迁移

.net - 文件夹重定向处于事件状态时解析 'Environment.SpecialFolder.Personal'

c# - 尝试上传到 Azure blob 时收到错误消息 "The TLS version of the connection is not permitted on this storage account."