C # How to obtain the time interval with SQL

Source: Internet
Author: User

TimeSpan usage

TimeSpan is used to represent an instance of a time period. The difference between two time periods can constitute a TimeSpan instance. Here are some important usage:

A first introduces several methods

TimeSpan. Minutes (other time, such as the number of days, Hours, and the number of seconds are the same.) Other Hours and Second are the same.

DateTime. Tick: it is a time cycle, which indicates one hundred nanoseconds, that is, one thousandth of a second. Here, Ticks indicates the total difference in time periods, that is, 9*24 * 3600*10000000 + 23*3600*10000000 + 59*60*10000000 + 59*10000000 = 8639990000000. 3600 is an hour In seconds

TimeSpan. TotalDays: the number of different days in two time periods, the same for other TotalHours, TotalMinutes, and TotalSeconds

B: difference in time

String time1 = "8:10:00 ";

String time2 = "18:20:00 ";

DateTime t1 = Convert. ToDateTime (time1 );

DateTime t2 = Convert. ToDateTime (time2 );

TimeSpan ts1 = t2-t1;

String tsMin = ts1.Minutes. ToString ();

TimeSpan ts11 = new TimeSpan (t1.Tick );

TimeSpan ts22 = new TimeSpan (t2.Tick );

String diff = ts22.Subtract (ts11). TotalMinutes. ToString ();

Subtract: the difference between two time periods.

Diff: represents the minutes of two different time periods. The example above is 610 minutes.


-- Cs code --

DateTime t1 = DateTime. Parse ("1998-2-25 ");
DateTime t2 = DateTime. Parse ("2009-7-28 ");
System. TimeSpan ts = t2-t1;
Int day = ts. Days;


-- SQL code --
Select year = datediff (year, '2014-2-25 ', '2014-7-28'), month = datediff (month, '2014-2-25 ', '2014-7-28 ')


Select month = datediff (month, '2017-2-25 ', '2017-7-28 ')


Select day = datediff (day, '2017-2-25 ', '2017-7-28 ')

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.