计算持续时间 [英] calculating time duration
问题描述
hi ..请检查我的这个代码:
protected void Button4_Click(object sender, EventArgs e) { DateTime dt1; DateTime dt2; string currenttime1 = DateTime.Now.ToLongTimeString(); string currenttime2 = DateTime.Now.ToLongTimeString(); TextBox2.Text = currenttime2; dt1 = Convert.ToDateTime(currenttime1); dt2 = Convert.ToDateTime(currenttime2); TimeSpan ts = dt1 - dt2; int hours = ts.Hours; int min = ts.Minutes; string finalMin = Convert.ToString(min); TextBox3.Text = finalMin;
----我有一个网页,可以计算用户按下停止按钮时的持续时间..但是此代码的输出始终为零..我的错误是什么?
在此先感谢.. :)
--im使用asp.net和C#:confused:
这是一个快速的控制台应用程序,您可以适应您的ASP.NET页面. "cs"> 使用系统; 命名空间 TimeDemo { class 程序 { 静态 无效 Main(字符串 []参数) { DateTime现在= DateTime.Now; 日期时间更早=现在.AddHours(-1); Console.WriteLine(" ,现在.ToLongTimeString()); Console.WriteLine(" ,更早.ToLongTimeString()); TimeSpan持续时间=现在-更早; Console.WriteLine(" ,duration.TotalMinutes); Console.ReadKey(); } } }
您正在比较几乎在同一实例中检索到的两次,当然,两者的差将为0
Heaven23写道:dt1 = Convert.ToDateTime(currenttime1);
dt2 = Convert.ToDateTime(currenttime2);
这一点绝对没用.您正在获取当前的DateTime,将其转换为字符串,然后转换回DateTime. :rolleyes:没用.
hi.. please check this code of mine:
protected void Button4_Click(object sender, EventArgs e) { DateTime dt1; DateTime dt2; string currenttime1 = DateTime.Now.ToLongTimeString(); string currenttime2 = DateTime.Now.ToLongTimeString(); TextBox2.Text = currenttime2; dt1 = Convert.ToDateTime(currenttime1); dt2 = Convert.ToDateTime(currenttime2); TimeSpan ts = dt1 - dt2; int hours = ts.Hours; int min = ts.Minutes; string finalMin = Convert.ToString(min); TextBox3.Text = finalMin;
---- i''m having a web page that calculated time duration when the user press the stop button.. but the output of this code is always zero.. what is my error?
thanks in advance.. :)
--im using asp.net and C# :confused:
This is a quick console application which you can adapt to your ASP.NET page.using System; namespace TimeDemo { class Program { static void Main(string[] args) { DateTime now = DateTime.Now; DateTime earlier = now.AddHours(-1); Console.WriteLine("Now: {0}", now.ToLongTimeString()); Console.WriteLine("Earlier: {0}", earlier.ToLongTimeString()); TimeSpan duration = now - earlier; Console.WriteLine("Duration {0} minutes", duration.TotalMinutes); Console.ReadKey(); } } }
You are comparing two times retrieved at virtually the same instance, of course the difference will be 0
Heaven23 wrote:dt1 = Convert.ToDateTime(currenttime1);
dt2 = Convert.ToDateTime(currenttime2);
This bit is absolutely useless. You are getting the current DateTime, converting it to a string, then converting back to a DateTime. :rolleyes: Useless.
这篇关于计算持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!