[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How do I compare the dates in text boxes.

Posted on 2011-02-21
11
Medium Priority
?
790 Views
Last Modified: 2013-12-17
How would I write this.  I’m trying to compare  the dates in two text boxes.  This is what I have, what’s wrong with it.

errors: invalid expression

Thanks!


string aa = TextBox1.Text.Trim();
        string bb = TextBox2.Text.Trim();
        if (aa =< bb)
        {
            Label6.Text = "Start date can not be earlier than today’s date";
            
        }

Open in new window

0
Comment
Question by:bmanmike39
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +5
11 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34948144
try this :
if (aa <= bb)
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34948184
however you would be better doing this :

string aa = TextBox1.Text.Trim(); 
        string bb = TextBox2.Text.Trim(); 

        datetime startDate;
        datetime endDate;
        startDate = new DateTime();
        endDate = new DateTime();
        startDate = DateTime.ParseExact(aa, "yyyy-MM-dd HH:mm tt", null);
        endDate = DateTime.ParseExact(bb, "yyyy-MM-dd HH:mm tt", null);
        if (DateTime.Compare(startDate,endDate) < 0 ) 
        { 
            Label6.Text = "Start date can not be earlier than today’s date"; 
             
        }

Open in new window


Here is a link explaining DateTime.Compare : http://msdn.microsoft.com/en-us/library/system.datetime.compare.aspx
0
 
LVL 13

Expert Comment

by:Rahul Agarwal
ID: 34948364
Another way of doing this through AJAX is :

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>”);

Note: 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”
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Expert Comment

by:ninusajil
ID: 34948529
DateTime startdate=DateTime.ParseExact(TextBox1.Text,"dd/mm/yyyy",null);
            DateTime enddate = DateTime.ParseExact(TextBox2.Text, "dd/mm/yyyy", null);
            if (enddate > startdate)
            {
                //process
            }
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 34949206
I would suggest you use Datetime picker controls for entering the dates on your form (unless there is a good reason they would not be suitable).  By using those it is impossible for the user to enter an incorrect date - with a text box one could enter, by mistake, 30.02.2010 (February never has 30 days)
0
 
LVL 8

Accepted Solution

by:
Anurag Agarwal earned 2000 total points
ID: 34950021
hello bmanmike39 !!

I think u only need this ..........

Dim dt1, dt2 As New DateTime
        dt1 = TextBox1.Text
        dt2 = TextBox2.Text
        If DateTime.Compare(dt1, dt2) = 0 Then
            Response.Write("Same Date Time")
        ElseIf DateTime.Compare(dt1, dt2) < 0 Then
            Response.Write("dt2 is bigger")
        ElseIf DateTime.Compare(dt1, dt2) > 0 Then
            Response.Write("dt1 is bigger")
        End If

0
 
LVL 7

Expert Comment

by:kovilpattiBalu
ID: 34950566
can use .. Convert.ToDateTime() also
0
 
LVL 7

Expert Comment

by:mr_nadger
ID: 34950782
go with Kovil's Convert suggestion, just be sure to validate the contents of the boxes first.
0
 

Author Comment

by:bmanmike39
ID: 34961546
DateTime startDate = DateTime.ParseExact(TextBox1.Text, "dd/mm/yyyy", null);
        DateTime endDate = DateTime.ParseExact(TextBox2.Text, "dd/mm/yyyy", null);
        if (endDate == startDate)
        {
            Label6.Text = "End date can not be equal to start date";
       }

Error:  String was not recognized as a valid DateTime.

0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 34961585
>>Error:  String was not recognized as a valid DateTime.

That is why I made my earlier comment:

I would suggest you use Datetime picker controls for entering the dates on your form (unless there is a good reason they would not be suitable).  By using those it is impossible for the user to enter an incorrect date - with a text box one could enter, by mistake, 30.02.2010 (February never has 30 days)
0
 

Author Closing Comment

by:bmanmike39
ID: 34987576
Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question