C # Simple method for calculating time intervals (recommended) _c# tutorial

Source: Internet
Author: User
Tags datetime time interval

First, there are two ways to calculate the time interval for two datetime:

1, two datetime direct subtraction, get a timespan, representing two time interval

2, with the Datetime.subtract (DateTime) method of calculation, the result is the same as the method 1, indicating that the current time minus the time of the parameter, return a TimeSpan

The timespan structure represents a time interval, measured by positive and negative days, hours, minutes, seconds, and decimal parts of seconds.

The text of the TimeSpan object (. ToString ()) "11.13:46:40", representing 11 days, 13 hours, 46 minutes, and 40 seconds.

Timespan.totalseconds property: Gets the value of the current TimeSpan structure, expressed as a fraction of the integer seconds and seconds, to a negative number. The days, hours, minutes, and seconds are converted to seconds to display.

TimeSpan days, Hours, minutes, seconds attributes: To obtain the day, hour, minute, and second of the time interval, respectively.

Here is an example: the values for TS and TS2 are the same.

DateTime dtimeend = examairtime.addminutes (examperiod);

DateTime dtimeserver = Dataclass.getdateserver ();

TimeSpan ts = dtimeend-dtimeserver;

TimeSpan ts2 = dtimeend.subtract (dtimeserver);

MessageBox.Show (TS. ToString () + "\ n" + ts2. Totalminutes.tostring ());

The above C # calculation of the time interval of the simple method (recommended) is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.