C # uses a TimeSpan to calculate two time difference

Source: Internet
Author: User

Reprint: http://www.cnblogs.com/wifi/articles/2439916.html

You can add two dates between any one unit of time.

private String DateDiff (DateTime DateTime1, DateTime DateTime2) {string DateDiff = null;   TimeSpan ts = datetime1.subtract (DateTime2). Duration (); DateDiff = ts. Days.tostring () + "Day" + TS. Hours.tostring () + "hours" + TS. Minutes.tostring () + "minutes" + TS. Seconds.tostring () + "seconds"; return DateDiff; }


Description
The 1.DateTime value type represents a specific date time from January 1, 01 0:0 0 seconds to the December 31, 9999 23:59 59 seconds. 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.

C # uses a TimeSpan to calculate two time difference

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.