winfrom:
private void dteStartDate_Validating(object sender, CancelEventArgs e)
{
if (!CheckStartDateEndDate(true))
{
XtraMessageBox.Show("起始日期不能大于结束日期");
dteStartDate.Focus();
e.Cancel = true;
}
}
private void dteEndDate_Validating(object sender, CancelEventArgs e)
{
if (!CheckStartDateEndDate(false))
{
XtraMessageBox.Show("结束日期不能小于起始日期");
dteEndDate.Focus();
e.Cancel = true;
}
}
private bool CheckStartDateEndDate(bool bLeaveFromStartDate)
{
if (!(string.IsNullOrEmpty(dteStartDate.Text) || string.IsNullOrEmpty(dteEndDate.Text)))
{
if (Convert.ToDateTime(dteStartDate.Text) > Convert.ToDateTime(dteEndDate.Text))
{
if (bLeaveFromStartDate)
{
return false;
}
else
{
return false;
}
}
}
return true;
}
web:
if (txtStartDate.Value==null || !PageValidate.IsDateTime(txtStartDate.Value.ToString()))
strErr+="请输入正确的开始日期!\n";
if (txtEndDate.Value == null || !PageValidate.IsDateTime(txtEndDate.Value.ToString()))
strErr += "请输入正确www.2cto.com的结束日期!\n";
if(Convert.ToDateTime(txtStartDate.Value.ToString()).CompareTo(Convert.ToDateTime(txtEndDate.Value.ToString()))>0)
{
strErr += "开始时间不能大于结束时间!\n";
}
/// <summary>
/// 日期格式字符串判断
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsDateTime(string str)
{
try
{
if (!string.IsNullOrEmpty(str))
{
DateTime.Parse(str);
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
转载自keenweiwei博客