"Reprint" C # time difference calculation, accurate output "Month day seconds"

Source: Internet
Author: User

======================== Thanks for "Don't forget Beginner's mind" The Great God share ========================

Original Blog Address: http://www.cnblogs.com/IT-Bear/archive/2012/01/16/2323609.html=========================================== ====================== [Code]phpcode:
<summary>///overloaded. Calculates a time interval of two dates, which returns the absolute value of the date difference for the interval. </summary>//<param name= "DateTime1" > First date and Time </param>//<param name= "DateTime 2 "> Second date and Time </param>//<returns></returns> private string DateDiff (DateTime datetime             1,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 () + "Day" +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 returns the absolute value of the date difference for the time interval. </summary>//<param name= "DateTime1" > A date and Time </param>//<returns></returns> private String DateDiff (DateTime DateTime1) {return this.         DateDiff (Datetime1,datetime.now); }
[Code]phpcode:
Label1. Text = "2004-1-1 15:36:05"; Label2. Text = "2004-3-1 20:16:35";D atetime d1 =new datetime (2004,1,1,15,36,05);D atetime d2 =new datetime (2004,3,1,20,16,35); TimeSpan D3 = D2. Subtract (D1); label3. Text = "Difference:" +d3. Days.tostring () + "Day" +d3. Hours.tostring () + "Hour" +d3. Minutes.tostring () + "minutes" +d3. Seconds.tostring () + "seconds";
------------------------------------------------Supplement Base------------------------------------------------------1. A datetime value type represents a specific date time from January 1, 01 0:0 0 seconds to 59 seconds from December 31, 9999. Therefore, you can use a DateTime value type to describe any time that is within the scope of your imagination. A datetime value represents a specific moment.
The 2.TimeSpan value contains many properties and methods for accessing or processing a TimeSpan value
The following list covers a subset of these:
Add: Adds to another timespan value.
Days: Returns the TimeSpan value that is calculated in day.
Duration: Gets the absolute value of the timespan.
Hours: Returns the TimeSpan value computed in hours
Milliseconds: Returns the TimeSpan value computed in milliseconds.
Minutes: Returns the TimeSpan value computed in minutes.
Negate: Returns the inverse number of the current instance.
Seconds: Returns the TimeSpan value computed in seconds.
Subtract: Subtracts another TimeSpan value from it.
Ticks: Returns the number of ticks for the timespan value.
Totaldays: Returns the number of days that the TimeSpan value represents.
TotalHours: Returns the number of hours that the timespan value represents.
TotalMilliseconds: Returns the number of milliseconds represented by a timespan value.
Totalminutes: Returns the number of minutes represented by a timespan value.
TotalSeconds: Returns the number of seconds represented by a timespan value.

"Reprint" C # time difference calculation, accurate output "Month day seconds"

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.