Run this code
HTML
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Start Date
</td>
<td>
<asp:TextBox ID="txtStartDate" runat="server" Text="04/06/2014" />
</td>
</tr>
<tr>
<td>
End Date
</td>
<td>
<asp:TextBox ID="txtEndDate" runat="server" Text="06/06/2014" />
</td>
</tr>
</table>
<table border="0" cellpadding="5" cellspacing="2">
<tr>
<td>
DateTime days count without culture =
</td>
<td>
<asp:Label ID="lblDaysCountWithoutCulture" runat="server" />
</td>
</tr>
<tr>
<td>
DateTime days count with culture =
</td>
<td>
<asp:Label ID="lblDaysCountWithCulture" runat="server" />
</td>
</tr>
</table>
<br />
<asp:Button Text="Find Number of Days" runat="server" OnClick="DaysBetweenDates" />
</div>
Namespace
using System.Globalization;
C#
protected void DaysBetweenDates(object sender, EventArgs e)
{
TimeSpan diff, diff1;
// This will give you 61 days
DateTime startDateWithoutCulture = DateTime.Parse(this.txtStartDate.Text);
DateTime endDateWithoutCulture = DateTime.Parse(this.txtEndDate.Text);
//This will give you correct answer.
int days1 = 0, days2 = 0;
DateTime startDateWithCultureInfo = Convert.ToDateTime(this.txtStartDate.Text, new CultureInfo("en-GB"));
DateTime endDateWithCultureInfo = Convert.ToDateTime(this.txtEndDate.Text, new CultureInfo("en-GB"));
if (endDateWithoutCulture > startDateWithoutCulture)
{
diff = endDateWithoutCulture.Subtract(startDateWithoutCulture);
days1 = Convert.ToInt16(diff.Days);
}
if (endDateWithCultureInfo > startDateWithCultureInfo)
{
diff1 = endDateWithoutCulture.Subtract(startDateWithCultureInfo);
days2 = Convert.ToInt16(diff1.Days);
}
this.lblDaysCountWithoutCulture.Text = days1.ToString();
this.lblDaysCountWithCulture.Text = days2.ToString();
}
Screenshot
