1, winform
[Csharp]
# Region calendar Control
Private void dteStartDate_Validating (object sender, CancelEventArgs e)
{
If (! CheckStartDateEndDate (true ))
{
XtraMessageBox. Show ("start date cannot be later than end date ");
DteStartDate. Focus ();
E. Cancel = true;
}
}
Private void dteEndDate_Validating (object sender, CancelEventArgs e)
{
If (! CheckStartDateEndDate (false ))
{
XtraMessageBox. Show ("the end date cannot be earlier than the start date ");
DteEndDate. Focus ();
E. Cancel = true;
}
}
# Endregion
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;
}
2. web
[Csharp]
If (txtStartDate. Value = null |! PageValidate. IsDateTime (txtStartDate. Value. ToString ()))
StrErr + = "enter the correct start date! \ N ";
If (txtEndDate. Value = null |! PageValidate. IsDateTime (txtEndDate. Value. ToString ()))
StrErr + = "Enter the end date of the correct www.2cto.com! \ N ";
If (Convert. ToDateTime (txtStartDate. Value. ToString (). CompareTo (Convert. ToDateTime (txtEndDate. Value. ToString ()> 0)
{
StrErr + = "the start time cannot be later than the end time! \ N ";
}
# Region date format judgment
/// <Summary>
/// Date Format String judgment
/// </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;
}
}
# Endregion
From the column keenweiwei