Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Default.aspx

ASP.NET
<table>
        <tr>
        <td>
        Star Date
        </td>
        
        <td>
         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
         <ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server"  Format="dd/MM/yyyy" TargetControlID="TextBox1"/>
        </td>
        </tr>
        <tr>
        <td>
        End Date
        </td>
        <td>
         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
       
        <ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" Format="dd/MM/yyyy" TargetControlID="TextBox2"/>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        </td>
        </tr>
        <tr>
        <td>
        Total Days
        </td>
        <td>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        </td>
        </tr>
        <tr>
        <td></td>
        <td>
         <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" 
                Width="82px" />
        </td>
        </tr>
        </table>


Default.aspx.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        String date1 = TextBox1.Text.ToString();
        String date2 = TextBox2.Text.ToString();
      
        DateTime dt1 = DateTime.ParseExact(date1, "dd/MM/yyyy", null);
        DateTime dt2 = DateTime.ParseExact(date2, "dd/MM/yyyy", null);

        TimeSpan span = dt2.Subtract(dt1);
        TextBox3.Text = span.ToString();
      
    }
}


Show Error
C#
String was not recognized as a valid DateTime.
Posted
Updated 21-Jan-16 23:16pm
v3
Comments
F-ES Sitecore 22-Jan-16 5:12am    
Using the debugger, what is in date1 and date2?

1 solution

ParseExact expects the string you pass it from the textbox to be in an exact format - no more, no less. So if the user enters "1/6/15", "01/06/15" or "22-01-2015" it will fail, and you will get the error.
Forcing users to enter in a specific format is normally not a good idea - it's best to let them enter in a format they are familiar with (and that matches the Culture setting on their machine) and then convert that with TryParse instead of using ParseExact.
And you don't need Subtract: minus will work just as well:
C#
string date1 = TextBox1.Text;
string date2 = TextBox2.Text;

DateTime dt1, dt2;
if (!DateTime.TryParse(date1, out dt1) || !DateTime.TryParse(date2, out dt2))
    {
    ... Report problem
    return;
    }

TimeSpan span = dt2 - dt1;
TextBox3.Text = span.TotalDays.ToString();


BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900