Use timespan in C # To calculate the difference between two times

Source: Internet
Author: User
Tags difference between two times
You can add any time unit between two dates.
Private string datediff (datetime datetime1, datetime datetime2)
{String datediff = NULL;
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 () + "Minute" + ts. seconds. tostring () + "seconds ";
Return datediff;
} It Information home http://www.it55.com

Note:
1. The datetime Value Type represents a specific date time between 00:00:00, January 1, January 1, 0001 AD and, January 1, December 31, 9999 AD. Therefore, you can use the datetime value type to describe any time within the expected range. A datetime value represents a specific time
2. the timespan value contains many attributes and methods used to access or process a timespan value.
the following list covers some of them:
Add: adds the value to another timespan value.
days: returns the timespan value calculated by the number of days.
Duration: obtains 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: 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 represented by the timespan value.

Related Article

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.