给定一个字符串时间,如何判断它是否是日期类型?

echo123321 2004-11-25 09:39:03
类似于vb里面的iddate函数
比如我想把2003-2-29这个串拼接起来以后。怎么来判断是否是有效的日期类型。
...全文
360 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
515189 2004-11-25
  • 打赏
  • 举报
回复
不好意思我没看清楚楼主的意思!
public static bool IsDate(string strChkParam)
{
if (strChkParam.Length == 0) {
return true;
}
Regex regex;
if (!IsHalfEisuuji(strChkParam)) {
return false;
}
string InputFormat1 = "YYYYMMDD";
string InputFormat2 = "YYYYMM";
string InputFormat3 = "MMDD";
string InputFormat4 = "YYYY/MM/DD";
string InputFormat5 = "YYYY/MM";
string InputFormat6 = "MM/DD";
if (strChkParam.Length == InputFormat4.Length) {
regex = new System.Text.RegularExpressions.Regex("^\d{4}/\d{2}/\d{2}$");
} else if (strChkParam.Length == InputFormat5.Length) {
regex = new System.Text.RegularExpressions.Regex("^\d{4}/\d{2}$");
} else if (strChkParam.Length == InputFormat6.Length) {
regex = new System.Text.RegularExpressions.Regex("^\d{2}/\d{2}$");
}
if (!(regex == null)) {
if ((!(regex.IsMatch(strChkParam)))) {
return false;
}
}
strChkParam = strChkParam.Replace("/", "");
if (IsNumber(strChkParam)) {
if (strChkParam.Length == InputFormat3.Length) {
try {
Convert.ToDateTime(DateTime.Now.Year.ToString() + "/" + strChkParam.Substring(0, 2) + "/" + strChkParam.Substring(2, 2));
} catch (Exception ex) {
return false;
}
} else if (strChkParam.Length == InputFormat2.Length) {
try {
Convert.ToDateTime(strChkParam.Substring(0, 4) + "/" + strChkParam.Substring(4, 2) + "/01");
} catch (Exception ex) {
return false;
}
} else if (strChkParam.Length == InputFormat1.Length) {
try {
Convert.ToDateTime(strChkParam.Substring(0, 4) + "/" + strChkParam.Substring(4, 2) + "/" + strChkParam.Substring(6, 2));
} catch (Exception ex) {
return false;
}
} else {
return false;
}
} else {
return false;
}
return true;
}
515189 2004-11-25
  • 打赏
  • 举报
回复
Public Shared Function IsDate(ByVal strChkParam As String) As Boolean
If strChkParam.Length = 0 Then Return True
Dim regex As Regex

If Not IsHalfEisuuji(strChkParam) Then
Return False
End If

Dim InputFormat1 As String = "YYYYMMDD"
Dim InputFormat2 As String = "YYYYMM"
Dim InputFormat3 As String = "MMDD"
Dim InputFormat4 As String = "YYYY/MM/DD"
Dim InputFormat5 As String = "YYYY/MM"
Dim InputFormat6 As String = "MM/DD"

Select Case strChkParam.Length
Case InputFormat4.Length
regex = New System.Text.RegularExpressions.Regex("^\d{4}/\d{2}/\d{2}$")
Case InputFormat5.Length
regex = New System.Text.RegularExpressions.Regex("^\d{4}/\d{2}$")
Case InputFormat6.Length
regex = New System.Text.RegularExpressions.Regex("^\d{2}/\d{2}$")
End Select

If Not (regex Is Nothing) Then
If (Not (regex.IsMatch(strChkParam))) Then
Return False
End If
End If

strChkParam = strChkParam.Replace("/", "")
If IsNumber(strChkParam) Then
Select Case strChkParam.Length
Case InputFormat3.Length
Try
Convert.ToDateTime(DateTime.Now.Year.ToString() + "/" + strChkParam.Substring(0, 2) + "/" + strChkParam.Substring(2, 2))
Catch ex As Exception
Return False
End Try
Case InputFormat2.Length
Try
Convert.ToDateTime(strChkParam.Substring(0, 4) + "/" + strChkParam.Substring(4, 2) + "/01")
Catch ex As Exception
Return False
End Try
Case InputFormat1.Length
Try
Convert.ToDateTime(strChkParam.Substring(0, 4) + "/" + strChkParam.Substring(4, 2) + "/" + strChkParam.Substring(6, 2))
Catch ex As Exception
Return False
End Try
Case Else
Return False
End Select
Else
Return False
End If

Return True

End Function
515189 2004-11-25
  • 打赏
  • 举报
回复
Public Shared Function IsNumber(ByVal strChkParam As String) As Boolean
If strChkParam.Length = 0 Then Return True
Dim regex As Regex
If IsHalfEisuuji(strChkParam) Then
regex = New System.Text.RegularExpressions.Regex("^\d*$")
If regex.IsMatch(strChkParam) And IsHalfEisuuji(strChkParam) Then
Return True
End If
End If

Return False

End Function
活靶子哥哥 2004-11-25
  • 打赏
  • 举报
回复
<%@Page Language="c#" Debug="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="gb2312" lang="gb2312">
<head>
<title> New Document </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="title" content="" />
<meta name="author" content="活靶子,Huobazi,www.AspxBoy.com" />
<meta name="subject" content="" />
<meta name="language" content="gb2312" />
<meta name="keywords" content="" />
<meta name="Copyright" content="www.AspxBoy.com" />
<meta name="robots" content="all" />
<script langauge="c#" runat="server">

void Page_Load(object o , EventArgs e)
{

if(!Page.IsPostBack)
{
Response.Write(IsDate("2003-2-28"));
Response.Write("<br />");
Response.Write(IsDate("2003-2-29"));
Response.Write("<br />");
Response.Write(IsDate("2003-222-29"));

}

}
/// <summary>
/// 检验字符串是否是合法的日期
/// </summary>
/// <param name="strDate">被检验的字符串</param>
/// <returns>bool</returns>

public bool IsDate(string strDate)
{
DateTime dtDate;
bool bValid = true;
try
{
dtDate = DateTime.Parse(strDate);
}
catch
(FormatException e)
{
bValid = false;
}
return bValid;
}


</script>
</head>
<body>
<form id="frm" runat="server">



</form>
</body>
</html>
ALLsharps 2004-11-25
  • 打赏
  • 举报
回复
呵呵,那个是VBscript中的
yelook 2004-11-25
  • 打赏
  • 举报
回复
To: rickjelly2004(rick & jelly)

有这样的函数吗
ALLsharps 2004-11-25
  • 打赏
  • 举报
回复
C#中好像是没有吧
ALLsharps 2004-11-25
  • 打赏
  • 举报
回复
这是我做的一个用户自定义涵数

你不要这样的功能吗?

给你编一个涵数用用!!!

rickjelly2004 2004-11-25
  • 打赏
  • 举报
回复
C#中没有ISDATE这个涵数????
ALLsharps 2004-11-25
  • 打赏
  • 举报
回复
//C#
public bool IsDate(string str)
{
bool Result=true;
try
{
System.Convert.ToDateTime(str);
}
catch
{
Result=false;
}
return Result;
}
echo123321 2004-11-25
  • 打赏
  • 举报
回复
我想问的是c#里面的解决方式 。对不起
rickjelly2004 2004-11-25
  • 打赏
  • 举报
回复
if IsDate(str) then
onlytiancai 2004-11-25
  • 打赏
  • 举报
回复
正则表达式啦,呱呱

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧