C # How does one calculate the time interval?

Source: Internet
Author: User
Time Difference Calculation
Method 1:
Use timespan.
Related attributes and functions
Add: adds the value to another timespan value.
Days: returns the timespan value calculated by the number of days.
Duration: Get timespan's Absolute Value .
Hours: returns the hourly timespan value.
Milliseconds: returns the timespan value in milliseconds.
Minutes: returns the timespan value calculated in minutes.
Negate: returns Reverse number .
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.
Simple Example:
Datetime d1 = new datetime );
Datetime D2 = new datetime );
Timespan D3 = d2.subtract (D1 );
Lbtime. Text = "difference :"
+ D3.days. tostring () + "day"
+ D3.hours. tostring () + "hour"
+ D3.minutes. tostring () + "Minute"
+ D3.seconds. tostring () + "seconds ";
Method 2: Use the datediff function in SQL
Usage: datediff (datepart, startdate, enddate)
It can help you get the time difference in various forms you want, such as how many days, how many hours, and how many minutes are separated. The specific format is as follows:
Abbreviation of date
Year YY, yyyy
Quarter QQ, Q
Month mm, m
Dayofyear dy, y
Day DD, d
Week wk, WW
Hour HH
Minute Mi, n
Second SS, S
Millisecond MS



Datetime is the built-in event type, and timespan is the built-in event interval type.
Perform operations on two datetime-type variables to obtain data of the timespan type.
Datetime. Now is a read-only attribute that returns the current time, type: datetime
Timespan: Based on Input Date Calculation The interval between two specified dates.
Syntax : String timespan (' Escape characters ',' Compared date', 'target compared date ')
Return Value: Character
Parameter description :
Escape Character: Characters of the input type with different formatting characters, as shown in the content description. The formatting characters in the input characters are formatted and output. Other characters that are not formatted are output according to the corresponding position. 'Date to be compared ': the input type is a character and the content of the character in the date format to be compared. 'Destination comparison date': the input type is a character used to compare the content of the characters in the target date format.
Description: Calculate the interval between two specified dates based on the input date.
Escape Character Description: T: obtains the difference value represented by the difference scale between two dates. (Positive or Negative ) D: returns the entire number of days for the difference between two dates. (Positive or negative) h: obtains the hour of the difference between two dates. (Positive or negative) U: returns the number of milliseconds for the difference between two dates. (Positive or negative) M: returns the number of minutes of the difference between two dates. (Positive or negative) S: returns the integer number of seconds between two dates. (Positive or negative) D: returns the value Decimal Partially represents the difference value. (Positive or negative) h: obtains the difference between two dates expressed in the fractional part of the hour and hour. (Positive or negative) U: obtains the difference between the entire millisecond count and the fractional part of the millisecond between two dates. (Positive or negative) M: gets the difference between two dates in the whole minute and minute decimal part. (Positive or negative) S: gets the difference between two dates in the integer and second decimal part. (Positive or negative)
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.