Sep 05 2009

Comparing two dates in Asp.net

In this tutorial I compare two dates in asp.net using two textbox controls and two ajax calender controls. You can use

asp.net calender controls instead of ajax calender controls but date formats must be same for both textbox’s date values i.e

(”dd/MM/yyyy” or “MM/dd/yyyy”)

Date Formats for Ajax CalendarExtender Control

Format=”MMMM d, yyyy”  Result is April 28, 1906

=”dd/MM/yyyy”    Result is 23/11/2008

=”MM/dd/yyyy”    Result is 12/28/2008

=”dd-MMM-yyyy” Result is 23-Mar-2008
Comparing Two dates

If second textbox1(DespatchOrder) date value is less than to first textbox2(OrderTaken) date value then alert box will

display “Check date values”.This TextBox2_TextChanged event is fired when textbox2 is less than textbox1 date value.

Default.aspx Page: When using ajax controls paste script manager in aspx pages top. Check Date Formats in ajax calender
must be in dd/MM/yyyy

Ajax CalendarExtender1 both PopupButtonID=”TextBox1″    and  TargetControlID=”TextBox1″

Ajax CalendarExtender2 both PopupButtonID=”TextBox2″    and  TargetControlID=”TextBox2″

When you click Textbox1 or Textbox2 Calender control will popup you can choose the dates from there

Design Code

<table style=”border: 1px solid #000000; font-family: Arial, Helvetica, sans-serif; font-size: 9pt; background-color:

#E6E6E6″>
<tr>
<td>OrderTaken</td>
<td>
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>

<cc1:CalendarExtender ID=”CalendarExtender1″ runat=”server” Format=”dd/MM/yyyy”
PopupButtonID=”TextBox1″ TargetControlID=”TextBox1″>
</cc1:CalendarExtender>

</td>
</tr>

<tr>
<td>
DespatchOrder</td>
<td><asp:TextBox ID=”TextBox2″ runat=”server” AutoPostBack=”True”
ontextchanged=”TextBox2_TextChanged”></asp:TextBox>

<cc1:CalendarExtender ID=”CalendarExtender2″ runat=”server” Format=”dd/MM/yyyy”
PopupButtonID=”TextBox2″ TargetControlID=”TextBox2″ >
</cc1:CalendarExtender>

</td>
</tr>

</table>

Default.aspx.cs code: Double click in TextBox2

protected void TextBox2_TextChanged(object sender, EventArgs e)
{
DateTime a = DateTime.Parse(TextBox1.Text);
DateTime b = DateTime.Parse(TextBox2.Text);

if (a > b)
{
Response.Write(”<script>alert(’Check date ranges to OrderTaken and OrderDespatch’);</script>”);
TextBox2.Text=””;
}
}

***Now Run the project Enter Textbox1 value as 09-02-2008

TextBox2 value as 02-02-2008

then textbox changed event fires and popup alert message “check date ranges to orderTaken and orderDespatch”

Note: You can write in this format also javascript alert message

Response.Write(”<script language=’javascript’>”);
Response.Write(”alert(’Please Check date ranges to OrderTaken and OrderDespatch.’);”);
Response.Write(”return false;”);
Response.Write(”<” + “/script>”);

dd_MMM_yyyyNote: You can compare date formats like above figure but change the ajax calender control formats i.e Format=”dd-MMM-yyyy”

<cc1:CalendarExtender ID=”CalendarExtender1″ runat=”server” Format=”dd-MMM-yyyy”
PopupButtonID=”TextBox1″ TargetControlID=”TextBox1″>
</cc1:CalendarExtender>

Similarly for calenderExtender2 Format=”dd-MMM-yyyy”

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Leave a Reply

Alibi3col theme by Themocracy