仅计算两个不同日期之间的天数 [英] Calculate only days between two different dates
问题描述
Default.aspx
Default.aspx
<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>
默认.aspx.cs
Default.aspx.cs
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
String was not recognized as a valid DateTime.
推荐答案
ParseExact期望从文本框传递的字符串格式正确 - 不多也不少。因此,如果用户输入1/6/15,01/06/15或22-01-2015,它将失败,您将收到错误。
强制用户以特定格式输入通常不是一个好主意 - 最好让他们以他们熟悉的格式输入(并匹配他们机器上的文化设置),然后使用TryParse而不是使用ParseExact进行转换。
你不需要减法:减去也会起作用:
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:
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:帮自己一个忙,停止使用Visual Studio的默认名称 - 你可能还记得今天的TextBox8是手机号码,但是当你必须在三周内修改它时,你会这样吗?使用描述性名称 - 例如tbMobileNo - 您的代码变得更容易阅读,更自我记录,更易于维护 - 并且编码速度更快,因为Intellisense可以通过三次击键来tbMobile,其中TextBox8需要思考大概和8次击键......
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...
这篇关于仅计算两个不同日期之间的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!