The decimal problem in C is solved
- 2020-05-10 18:39:05
- OfStack
Question:
startTime = DateTime.Now;
-----------
slExecutedTime.Text = (DateTime.Now - startTime).ToString();
Execution results:
Executed: 00:00:03.1234434 (lots of decimal places later)
Desired execution results:
Executed: 00:00:03
--------------------------------------------------------------------------------
Solution 1 (recommended) :
Related properties of TimeSpan:
Related properties and functions
Add : with the other 1 a TimeSpan Value added.
Days: Returns the number of days TimeSpan Value.
Duration: To obtain TimeSpan The absolute value of theta.
Hours: Returns in hours TimeSpan value
Milliseconds: Returns in milliseconds TimeSpan Value.
Minutes: Returns in minutes TimeSpan Value.
Negate: Returns the negative of the current instance.
Seconds: Returns in seconds TimeSpan Value.
Subtract: Subtract another from that 1 a TimeSpan Value.
Ticks: return TimeSpan The value of the tick The number.
TotalDays: return TimeSpan Value represents the number of days.
TotalHours: return TimeSpan Value represents the number of hours.
TotalMilliseconds: return TimeSpan Value represents the number of milliseconds.
TotalMinutes: return TimeSpan The number of minutes represented by the value.
TotalSeconds: return TimeSpan Value represents the number of seconds.
/// <summary>
/// Program execution time test
/// </summary>
/// <param name="dateBegin"> The start time </param>
/// <param name="dateEnd"> The end of time </param>
/// <returns> return ( seconds ) Units, such as : 0.00239 seconds </returns>
public static string ExecDateDiff(DateTime dateBegin, DateTime dateEnd)
{
TimeSpan ts1 = new TimeSpan(dateBegin.Ticks);
TimeSpan ts2 = new TimeSpan(dateEnd.Ticks);
TimeSpan ts3 = ts1.Subtract(ts2).Duration();
// You want to transfer the format
return ts3.TotalMilliseconds.ToString();
}
That's the basic thing. You get milliseconds
If you just want the format that you want, you can just take the first 10 bits
ts3.ToString("g") 0:00:07.171
ts3.ToString("c") 00:00:07.1710000
ts3.ToString("G") 0:00:00:07.1710000
There are 3 formats to choose from, and I recommend that you use a truncated trial if you need one
Such as
ts3.ToString("g").Substring(0,8) 0:00:07.1
ts3.ToString("c").Substring(0,8) 00:00:07
ts3.ToString("G").Substring(0,8) 0:00:00
Option 2: more complicated
#region Return time difference
public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
{
string dateDiff = null;
try
{
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
string hours = ts.Hours.ToString(), minutes = ts.Minutes.ToString(),seconds = ts.Seconds.ToString();
if(ts.Hours<10)
{
hours = "0" + ts.Hours.ToString();
}
if (ts.Minutes<10)
{
minutes = "0" + ts.Minutes.ToString();
}
if(ts.Seconds<10)
{
seconds = "0" + ts.Seconds.ToString();
}
dateDiff = hours + ":"+ minutes + ":"+ seconds;
}
catch
{
}
return dateDiff;
}
#endregion
From: http: / / www. cnblogs. com hongfei/archive 2013/03/11/2953366. html