C # method for calculating the time interval

Source: Internet
Author: User

1. Calculate the time interval between two datetime values.Two methods:

1. Two datetime values are directly subtracted, and a timespan is obtained, indicating the interval between the two datetime values.

2. Use the datetime. Subtract (datetime) method for calculation. The result is the same as that of method 1, indicating that the current time is used minus the time entered by the parameter, and a timespan is returned.

 

II,TimespanThe structure indicates a time interval, which is measured by plus or minus days, hours, minutes, seconds, and decimals of seconds.

TimespanObject text (. tostring () "11.13: 46: 40", indicating 11 days, 13 hours, 46 minutes, and 40 seconds.

Timespan. totalseconds attribute: gets the value of the current timespan structure expressed in the integer and second decimal parts, which can be negative. Converts days, hours, minutes, And seconds to seconds for display.

The days, hours, minutes, And seconds attributes of timespan are used to obtain the days, hours, minutes, And seconds of the time interval.

 

The following is an example: TS and ts2.

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 ());

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.