Calculation of time difference in C #

Source: Internet
Author: User

         <summary>///has been overloaded. Calculates an interval of two dates, returning the absolute value of the date difference for the interval. </summary>///<param name= "DateTime1" > First date and Time </param>///<param name= "Dateti Me2 "> Second date and time </param>///<returns></returns> private string DateDiff (DateTime date
             Time1,datetime DateTime2) {string datediff=null;
                 try {TimeSpan ts1=new TimeSpan (datetime1.ticks);
                 TimeSpan ts2=new TimeSpan (datetime2.ticks); TimeSpan Ts=ts1. Subtract (TS2).
                 Duration (); Datediff=ts. Days.tostring () + "Days" +ts. Hours.tostring () + "Hour" +ts. Minutes.tostring () + "minutes" +ts.
             Seconds.tostring () + "seconds";
         Catch {} return dateDiff; ///<summary>///has been overloaded. Calculates the time interval between a time and the current local date and time, and returnsis the absolute value of the date difference for the time interval. </summary>///<param name= "DateTime1" > A date and time </param>///<returns></ret Urns> private String DateDiff (DateTime DateTime1) {return this.
         DateDiff (Datetime1,datetime.now); }

Note:

The 1.DateTime value type represents a specific date time from January 1, 01 0:0 0 seconds to A.D. December 31, 9999 23:59 59 seconds. Therefore, you can use the DateTime value type to describe any time within the imaginary range. A datetime value represents a specific moment.
The 2.TimeSpan value contains a number of properties and methods that are used to access or process a TimeSpan value
The following list covers a subset of these:
Add: Adds to another timespan value.
Days: Returns the value of the timespan that is calculated in the day.
Duration: Gets the absolute value of the timespan.
Hours: Returns the timespan value calculated in hours
Milliseconds: Returns the TimeSpan value computed 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: Subtracts another timespan value from it.
Ticks: Returns the number of tick for the timespan value.
Totaldays: Returns the number of days that the TimeSpan value represents.
Totalhours: Returns the number of hours represented by the timespan value.
TotalMilliseconds: Returns the number of milliseconds represented by the timespan value.
Totalminutes: Returns the number of minutes represented by the timespan value.
TotalSeconds: Returns the number of seconds that the timespan value represents.


Label1. Text = "2004-1-1 15:36:05";
Label2. Text = "2004-3-1 20:16:35";

DateTime d1 =new datetime (2004,1,1,15,36,05);
DateTime d2 =new datetime (2004,3,1,20,16,35);

TimeSpan D3 = D2. Subtract (D1);

Label3. Text = "Difference:"
+d3. Days.tostring () + "Days"
+d3. Hours.tostring () + "Hour"
+d3. Minutes.tostring () + "minutes"
+d3. Seconds.tostring () + "seconds";



Source: http://www.cnblogs.com/IT-Bear/archive/2012/01/16/2323609.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.