后台如何判断时间?

taylermail 2008-07-29 02:42:14
一个文本框,一个按扭
要在按扭里写事件,判断文本框是否输入为时间?该如何写?没有isdate()函数嘛

if (isdate(cbsj.Text))
{
....
}
else{....}

有错
...全文
115 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
我姓区不姓区 2008-07-29
  • 打赏
  • 举报
回复
DateTime result=new DateTime();
if (DateTime.TryParse(TextBox1.Text, out result))
{ }
suyiming 2008-07-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ericzhangbo1982111 的回复:]
public bool IsDate(string time)
{
bool isDate=false;
try
{
DateTime dt=Convert.ToDateTime(time);
isDate=true;
}catch(Execption ex){isDate=false;}
return isDate;
}
[/Quote]
meimen2 2008-07-29
  • 打赏
  • 举报
回复
要自己写函数,MS没带ISDATE,只有个判断是否是闰年的函数
ccycat 2008-07-29
  • 打赏
  • 举报
回复
可在前台用正规表达式判断是否为日期格式
yangpeiyu 2008-07-29
  • 打赏
  • 举报
回复


/// <summary>
/// 是否为日期型字符串
/// </summary>
/// <param name="StrSource">日期字符串(2008-05-08)</param>
/// <returns></returns>
public static bool IsDate(string StrSource)
{
return Regex.IsMatch(StrSource, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
}

/// <summary>
/// 是否为时间型字符串
/// </summary>
/// <param name="source">时间字符串(15:00:00)</param>
/// <returns></returns>
public static bool IsTime(string StrSource)
{
return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
}

/// <summary>
/// 是否为日期+时间型字符串
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsDateTime(string StrSource)
{
return Regex.IsMatch(StrSource, @"^(((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$ ");
}
ericzhangbo1982111 2008-07-29
  • 打赏
  • 举报
回复
public bool IsDate(string time)
{
bool isDate=false;
try
{
DateTime dt=Convert.ToDateTime(time);
isDate=true;
}catch(Execption ex){isDate=false;}
return isDate;
}
wdgphc 2008-07-29
  • 打赏
  • 举报
回复
try
{
DateTime dt=Convert.ToDateTime(cbsj.Text);
return true;
}
catch
{
return false;
}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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