日期:2014-05-18  浏览次数:20873 次

时间差
DateTime login_time = Convert.ToDateTime(DateTime.Now.ToString("hh:mm:ss"));  
DateTime submit_time = Convert.ToDateTime(DateTime.Now.ToString("hh:mm:ss"));  
  TimeSpan sj = submit_time - login_time;


按说 sj显示的应该是05:04:16才对!为什么多出来前面的一长串啊!

------解决方案--------------------
private string DateDiff(DateTime DateTime1, DateTime DateTime2)
 {string dateDiff = null; 
 TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
 TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
 TimeSpan ts = ts1.Subtract(ts2).Duration();
 dateDiff = ts.Days.ToString()+"天"+ ts.Hours.ToString()+"小时"+ ts.Minutes.ToString()+"分钟"+ ts.Seconds.ToString()+"秒";
 return dateDiff;
 }
------解决方案--------------------
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
HiddenField1.Value = DateTime.Now.Ticks.ToString();
}

}

protected void Button1_Click(object sender, EventArgs e)
{
DateTime login_time = new DateTime(Convert.ToInt64(HiddenField1.Value));
DateTime submit_time = DateTime.Now;
TimeSpan ts = submit_time - login_time;
Response.Write("时间间隔(秒):" + ts.Seconds.ToString());
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html>