Solve the decimal problem in the calculation time difference in C #

Source: Internet
Author: User

Problem:

StartTime = DateTime. Now;
-----------
SlExecutedTime. Text = (DateTime. Now-startTime). ToString ();
Execution result:
Executed: 00: 00: 03.1234434 (there will be more decimals later)
Expected execution result:
Executed: 00: 00: 03

--------------------------------------------------------------------------------

Solution 1 (recommended ):

TimeSpan attributes:

Copy codeThe Code is as follows: related attributes and functions
Add: adds the value to another TimeSpan value.
Days: returns the TimeSpan value calculated by the number of Days.
Duration: gets the absolute value of TimeSpan.
Hours: returns the hourly TimeSpan value.
Milliseconds: returns the TimeSpan value in Milliseconds.
Minutes: returns the TimeSpan value calculated in Minutes.
Negate: returns the opposite number of the current instance.
Seconds: returns the TimeSpan value calculated in Seconds.
Subtract: Subtract another TimeSpan value from it.
Ticks: the number of tick values returned for TimeSpan.
TotalDays: returns the number of days indicated by the TimeSpan value.
TotalHours: returns the number of hours represented by the TimeSpan value.
TotalMilliseconds: the number of milliseconds in which the TimeSpan value is returned.
TotalMinutes: returns the number of minutes represented by the TimeSpan value.
TotalSeconds: returns the number of seconds represented by the TimeSpan value.

Copy codeThe Code is as follows: // <summary>
/// Test the program execution time
/// </Summary>
/// <Param name = "dateBegin"> Start time </param>
/// <Param name = "dateEnd"> end time </param>
/// <Returns> return (seconds), for example, 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 ();
// The format you want to convert
Return ts3.TotalMilliseconds. ToString ();
}

This is the most basic and the result is the number of milliseconds.
If you only need your format, you can simply take the first 10 digits.

Ts3.ToString ("g") 0:00:07. 171
Ts3.ToString ("c") 00:00:07. 1710000
Ts3.ToString (& quot; G & quot;) 0: 00: 00: 07.1710000
There are three formats to choose from. It is recommended that you use the truncation method if you need one.
For example

Copy codeThe Code is as follows: ts3.ToString ("g"). Substring () 0:00:07. 1
Ts3.ToString ("c"). Substring () 00:00:07
Ts3.ToString ("G"). Substring () 0:00:00

Solution 2: complicated

Copy codeCode: # 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.