检查一个字符串是使用DateTime.TryParse有效日期 [英] Check if a string is a valid date using DateTime.TryParse

查看:155
本文介绍了检查一个字符串是使用DateTime.TryParse有效日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 DateTime.TryParse()函数来检查,如果一个特定的字符串是不依赖于任何文化的有效日期时间。
令我惊讶的是,在函数返回甚至字符串,如1-1,1/1.etc。

I am using DateTime.TryParse() function to check if a particular string is a valid datetime not depending on any cultures.
To my surprise , the function returns true for even strings like "1-1", "1/1" .etc.

我该如何解决这个问题呢?

How can I solve this problem?

更新:

这是否意味着,如果我想检查是否有特定的字符串是有效的日期时间,我需要一个庞大的格式排列?将有不同的组合,我相信。
即使有大量的日期分隔符的(/,。' - ',等等。)根据培养,这将是困难的,我来定义格式要检查的阵列。
基本上,我想检查是否有特定的字符串至少包含一天(1至31或01至31),月(1至12或01至12)和年(yyyy或YY)以任意顺序,与任何日期分隔符,会有怎样的解决方案?
所以,如果该值包括时间任何地方,则它应该返回真太。 我能不能够定义格式的数组。

Does it mean, if I want to check if a particular string is valid datetime, I need a huge array of formats?? There will be different combinations , I believe.
Even there are lots of date separator ( '.' , '/' , '-', etc..) depending on the culture, it will be difficult for me to define an array of format to check against .
Basically, I want to check if a particular string contains AT LEAST day(1 through 31 or 01 through 31),month(1 through 12 or 01 through 12) and year(yyyy or yy) in any order, with any date separator , what will be the solution?
So, if the value includes any parts of time, it should return true too. I could NOT be able to define a array of format.

推荐答案

如果您希望您的日期,以符合特定的格式或格式,然后使用的 DateTime.TryParseExact 否则就是 DateTime.TryParse

If you want your dates to conform a particular format or formats then use DateTime.TryParseExact otherwise that is the default behaviour of DateTime.TryParse

DateTime.TryParse

该方法尝试为忽略不可识别的数据,如果可能的话,和   填写缺少的月,日和年信息与​​当前   日期。如果s只包含日期而没有时间,则此方法假设   时间是午夜12点。如果s包括日期组分与   两位数的年份,它被转换为一年,在当前文化的   基于所述Calendar.TwoDigitYearMax的值当前日历   属性。以s任何领先的,内在的,或尾随空白字符   被忽略。

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方法(字符串,字符串[] ,IFormatProvider的,DateTimeStyles,日期时间)超载​​。从同一链路示例:

If you want to confirm against multiple formats then look at DateTime.TryParseExact Method (String, String[], IFormatProvider, DateTimeStyles, DateTime) overload. Example from the same link:

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.

这篇关于检查一个字符串是使用DateTime.TryParse有效日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆