验证世界各国日期的类-正则表达式验证。
2010-04-07 10:50 北冥有魚,其名為坤、 阅读(392) 评论(0) 编辑 收藏 举报
代码
/// <summary>
/// 验证日期字符串是否合法
/// </summary>
/// <param name="sDate">要验证的日期字符串</param>
/// <param name="sCultrue">日期格式,所处的语言环境,dotNet的CultrueInfo的参数 :zh-CN</param>
/// <returns>是否通过验证</returns>
public static bool IsDate(string sDate, string sCultrue)
{
string sTmpDate = "";
bool bRight = false;
switch (sCultrue)
{
case "Machine": //yyyyMMdd TimeSpan
if (IsValidData(sDate, @"^(19\d{2}|20\d{2})(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$"))
{
bRight = true;
sTmpDate = sDate;
}
break;
case "zh-CN": //yyyy-MM-dd 中文 - 中國
if (IsValidData(sDate, @"^(19\d{2}|20\d{2})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$"))
{
bRight = true;
sTmpDate = sDate.Replace("-", "");
}
break;
case "zh-TW": //yyyy/MM/dd 中文 - 台灣
if (IsValidData(sDate, @"^(19\d{2}|20\d{2})\/(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])$"))
{
bRight = true;
sTmpDate = sDate.Replace("/", "");
}
break;
case "en-GB": //dd/MM/yyyy 英文 - 英國
if (IsValidData(sDate, @"^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/(19\d{2}|20\d{2})$"))
{
bRight = true;
sTmpDate = sDate.Replace("/", "");
sTmpDate = sTmpDate.Substring(4) + sTmpDate.Substring(2, 2) + sTmpDate.Substring(0, 2);
}
break;
case "fr-FR": //dd/MM/yyyy 法文 - 法國
if (IsValidData(sDate, @"^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/(19\d{2}|20\d{2})$"))
{
bRight = true;
sTmpDate = sDate.Replace("/", "");
sTmpDate = sTmpDate.Substring(4) + sTmpDate.Substring(2, 2) + sTmpDate.Substring(0, 2);
}
break;
case "de-DE": //dd.MM.yyyy 德文 - 德國
if (IsValidData(sDate, @"^(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[0-2])\.(19\d{2}|20\d{2})$"))
{
bRight = true;
sTmpDate = sDate.Replace(".", "");
sTmpDate = sTmpDate.Substring(4) + sTmpDate.Substring(2, 2) + sTmpDate.Substring(0, 2);
}
break;
case "en-US": //MM/dd/yyyy 英文 - 美國
if (IsValidData(sDate, @"^(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])\/(19\d{2}|20\d{2})$"))
{
bRight = true;
sTmpDate = sDate.Replace("/", "");
sTmpDate = sTmpDate.Substring(4) + sTmpDate.Substring(0, 2) + sTmpDate.Substring(2, 2);
}
break;
}
if (bRight == false || !IsDateStamp(sTmpDate))
return false;
return true;
}
/// <summary>
/// 判断日期字符串是否是DateStamp类型
/// </summary>
/// <param name="sDate">要验证的日期字符串</param>
/// <returns>是否通过验证</returns>
public static bool IsDateStamp(string sDate)
{
bool isRight = true;
if (IsValidData(sDate, @"^(19\d{2}|20\d{2})(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$"))
{
int iYear = int.Parse(sDate.Substring(0, 4));
int iMonth = int.Parse(sDate.Substring(4, 2));
int iDay = int.Parse(sDate.Substring(6));
// 為小月時
if ((iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11) && iDay > 30)
isRight = false;
//為閏年時
else if (iMonth == 2 && IsLeapYear(iYear) && iDay > 29)
isRight = false;
// 為平年時
else if (iMonth == 2 && !IsLeapYear(iYear) && iDay > 28)
isRight = false;
}
else
isRight = false;
return isRight;
}
/// <summary>
/// 验证是否是日期闰年
/// </summary>
/// <param name="iYear">年份</param>
/// <returns>是否通过验证</returns>
public static bool IsLeapYear(int iYear)
{
if (
((iYear % 100 != 0) && (iYear % 4 == 0)) ||
((iYear % 100 == 0) && (iYear % 400 == 0))
)
return true;
else
return false;
}
/// 验证日期字符串是否合法
/// </summary>
/// <param name="sDate">要验证的日期字符串</param>
/// <param name="sCultrue">日期格式,所处的语言环境,dotNet的CultrueInfo的参数 :zh-CN</param>
/// <returns>是否通过验证</returns>
public static bool IsDate(string sDate, string sCultrue)
{
string sTmpDate = "";
bool bRight = false;
switch (sCultrue)
{
case "Machine": //yyyyMMdd TimeSpan
if (IsValidData(sDate, @"^(19\d{2}|20\d{2})(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$"))
{
bRight = true;
sTmpDate = sDate;
}
break;
case "zh-CN": //yyyy-MM-dd 中文 - 中國
if (IsValidData(sDate, @"^(19\d{2}|20\d{2})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$"))
{
bRight = true;
sTmpDate = sDate.Replace("-", "");
}
break;
case "zh-TW": //yyyy/MM/dd 中文 - 台灣
if (IsValidData(sDate, @"^(19\d{2}|20\d{2})\/(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])$"))
{
bRight = true;
sTmpDate = sDate.Replace("/", "");
}
break;
case "en-GB": //dd/MM/yyyy 英文 - 英國
if (IsValidData(sDate, @"^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/(19\d{2}|20\d{2})$"))
{
bRight = true;
sTmpDate = sDate.Replace("/", "");
sTmpDate = sTmpDate.Substring(4) + sTmpDate.Substring(2, 2) + sTmpDate.Substring(0, 2);
}
break;
case "fr-FR": //dd/MM/yyyy 法文 - 法國
if (IsValidData(sDate, @"^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/(19\d{2}|20\d{2})$"))
{
bRight = true;
sTmpDate = sDate.Replace("/", "");
sTmpDate = sTmpDate.Substring(4) + sTmpDate.Substring(2, 2) + sTmpDate.Substring(0, 2);
}
break;
case "de-DE": //dd.MM.yyyy 德文 - 德國
if (IsValidData(sDate, @"^(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[0-2])\.(19\d{2}|20\d{2})$"))
{
bRight = true;
sTmpDate = sDate.Replace(".", "");
sTmpDate = sTmpDate.Substring(4) + sTmpDate.Substring(2, 2) + sTmpDate.Substring(0, 2);
}
break;
case "en-US": //MM/dd/yyyy 英文 - 美國
if (IsValidData(sDate, @"^(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])\/(19\d{2}|20\d{2})$"))
{
bRight = true;
sTmpDate = sDate.Replace("/", "");
sTmpDate = sTmpDate.Substring(4) + sTmpDate.Substring(0, 2) + sTmpDate.Substring(2, 2);
}
break;
}
if (bRight == false || !IsDateStamp(sTmpDate))
return false;
return true;
}
/// <summary>
/// 判断日期字符串是否是DateStamp类型
/// </summary>
/// <param name="sDate">要验证的日期字符串</param>
/// <returns>是否通过验证</returns>
public static bool IsDateStamp(string sDate)
{
bool isRight = true;
if (IsValidData(sDate, @"^(19\d{2}|20\d{2})(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$"))
{
int iYear = int.Parse(sDate.Substring(0, 4));
int iMonth = int.Parse(sDate.Substring(4, 2));
int iDay = int.Parse(sDate.Substring(6));
// 為小月時
if ((iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11) && iDay > 30)
isRight = false;
//為閏年時
else if (iMonth == 2 && IsLeapYear(iYear) && iDay > 29)
isRight = false;
// 為平年時
else if (iMonth == 2 && !IsLeapYear(iYear) && iDay > 28)
isRight = false;
}
else
isRight = false;
return isRight;
}
/// <summary>
/// 验证是否是日期闰年
/// </summary>
/// <param name="iYear">年份</param>
/// <returns>是否通过验证</returns>
public static bool IsLeapYear(int iYear)
{
if (
((iYear % 100 != 0) && (iYear % 4 == 0)) ||
((iYear % 100 == 0) && (iYear % 400 == 0))
)
return true;
else
return false;
}
调用此类,类名.isDate("要验证的字符串","zh-CN");