在我的TimeIn.aspx文件中,我使用以下代码显示时钟:
<div>
<div>
<asp:ScriptManager runat="server" ID="ScriptManager1" />
<br />
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<asp:Label runat="server" ID="Label4" Text="Current Time: "/>
<asp:Label runat="server" ID="Label2" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000" />
</div>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Check In" OnClick="CheckIn" />
</div>这个钟走得很好。然后在TimeIn.aspx.cs文件中,我编写了CheckIn方法:
protected void CheckIn(object sender, EventArgs e)
{
TimeSpan currtime = TimeSpan.Parse(Label2.Text);
int eid = Convert.ToInt32(Session["EID"]);
DBClient = new DBConnection();
DBClient.CheckIn(eid, currtime, DateTime.Now.Date.ToString());
Response.Redirect("ECheckin.aspx");
}在数据库中,CheckinTime列的数据类型为Time(7)。
当CheckIn事件触发时,它在TimeSpan.Parse的第一行给出异常,因为Label2.Text添加了带有时间格式的时间(AM/PM)。
Label2的样本值为:1:41:28 PM
处理这种情况的最佳解决方案是什么?我真的很想在sql server中使用Time数据类型,因为稍后我必须对时间字段执行计算。
发布于 2012-06-06 16:48:36
Timespan在两个时间之间基本上是不同的。
或者我们可以说秒表,其中秒表中的值是从时钟开始和停止以来已经过去的时间。
它与AM或PM无关
Timespan = Date1 - Date2我猜你得到的错误应该是FormatException
您的标签文本格式为DateTime,这就是AM/PM的原因。
尝试使用DateTime实例而不是时间跨度
喜欢
DateTime currtime = DateTime.Parse(Label2.Text);发布于 2014-07-18 09:48:55
您可以像下面这样向datetime添加timespan
TimeSpan timespan = new TimeSpan(03,00,00);
DateTime time = DateTime.Today.Add(timespan);
string displayTime = time.ToString("hh:mm tt");您可以直接传递timespan变量,而不是03,00,00
https://stackoverflow.com/questions/10910825
复制相似问题