该方法可以验证带格式的的日期
格式在:string[] date = strIn.Split(new char[]{'格式'},格式可以指定,比如‘-’,则要验证的日期必须符合2006-10-10这种格式。
//
验证 年-月-日
public static bool IsValidDate( string strIn)
{
bool result = false;
if(strIn.Length <= 10 && strIn.Length >=8)
{
string[] date = strIn.Split(new char[]{'-'});
if(date.Length == 3)
{
try
{
int year = Convert.ToUInt16(date[0],10);
int month = Convert.ToInt16(date[1],10);
int day = Convert.ToInt16(date[2],10);
result = CheckIsValidDate(day,month,year);
}
catch
{
result = false;
}
}
}
return result;
}
public static bool IsValidDate( string strIn)
{
bool result = false;
if(strIn.Length <= 10 && strIn.Length >=8)
{
string[] date = strIn.Split(new char[]{'-'});
if(date.Length == 3)
{
try
{
int year = Convert.ToUInt16(date[0],10);
int month = Convert.ToInt16(date[1],10);
int day = Convert.ToInt16(date[2],10);
result = CheckIsValidDate(day,month,year);
}
catch
{
result = false;
}
}
}
return result;
}
private
static
bool
CheckIsValidDate(
int
day,
int
month,
int
year)
{
if(month > 12 || month < 1)
return false;
if(day > 31 || day < 1)
return false;
if(day > DateTime.DaysInMonth(year, month))
return false;
return true;
}
{
if(month > 12 || month < 1)
return false;
if(day > 31 || day < 1)
return false;
if(day > DateTime.DaysInMonth(year, month))
return false;
return true;
}