C # TimeSpan application for Calculating Time Difference

Source: Internet
Author: User

C # Time Difference Calculation focus:

C # application of the time difference calculation function TimeSpan

The TimeSpan value contains many attributes and methods for accessing or processing 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: 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.

C # how to calculate the time difference:

 

  1. DateTime DateTime1,
  2. DateTime2 = DateTime. Now; // current time
  3. DateTime1 = Convert. ToDateTime ("20:00:00"); // set the required time minus
  4. StringDateDiff =Null;
  5. TimeSpan ts1 =NewTimeSpan (DateTime1.Ticks );
  6. TimeSpan ts2 =NewTimeSpan (DateTime2.Ticks );
  7. TimeSpan ts = ts1.Subtract (ts2). Duration ();
  8. // Display time
  9. DateDiff = ts. Days. ToString () + "day"
  10. + Ts. Hours. ToString () + "hour"
  11. + Ts. Minutes. ToString () + "Minute"
  12. + Ts. Seconds. ToString () + "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.