0

在我的 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.Text 的示例值为:1:41:28 PM
处理这种情况的最佳解决方案是什么?我真的很想在 sql server 中使用Time数据类型,因为稍后我将不得不对时间字段执行计算。

4

2 回答 2

3

时间跨度基本上是两次之间的差异。

或者我们可以说秒表,其中秒表中的值是自时钟开始和时钟停止以来经过的时间。

它与上午或下午无关。

Timespan = Date1 - Date2

我猜你得到的错误将是FormatException

您的标签文本格式DateTime就是 AM/PM 的原因。

而不是 Timespan 尝试使用DateTime实例

DateTime currtime = DateTime.Parse(Label2.Text);
于 2012-06-06T08:48:35.747 回答
0

您可以像下面那样将时间跨度添加到日期时间

  TimeSpan timespan = new TimeSpan(03,00,00);
    DateTime time = DateTime.Today.Add(timespan);
    string displayTime = time.ToString("hh:mm tt");

而不是 03,00,00 你可以直接传递你的时间跨度变量

于 2014-07-18T01:48:54.930 回答