SoFunction
Updated on 2025-03-01

How to get time intervals in c# and sql

How to use TimeSpan

TimeSpan is used to represent an instance of a time period. The difference between two times can form a TimeSpan instance. Now let me briefly introduce some important usages:

a. Let me introduce some methods first

(The difference in the number of minutes obtained when other times, such as days, hours, and seconds are the same), the same as other Hours and Second

: is a time period, representing one hundred nanoseconds, that is, one ten millionth of a second. Then Ticks here indicates how many time periods there are in total, i.e.: 9 * 24 *

: The number of days that differ between two time periods, the other TotalHours, TotalMinutes, and TotalSeconds are the same


b The difference between two times

Copy the codeThe code is as follows:

string time1 = "2010-5-26 8:10:00";

string time2 = "2010-5-26 18:20:00";

DateTime t1 = (time1);

DateTime t2 = (time2);

TimeSpan ts1=t2-t1;

string tsMin=();

TimeSpan ts11=new TimeSpan();

TimeSpan ts22=new TimeSpan();

string diff=(ts11).();


Subtract: indicates the difference between two time periods

diff: represents the number of minutes between two times, the above example is 610 minutes.

--cs code-

Copy the codeThe code is as follows:

DateTime   t1   =   ("1998-2-25 ");  
DateTime   t2   =   ("2009-7-28");  
   ts=t2-t1;  
int   day   =   ;

--sql code-

Copy the codeThe code is as follows:

select year=datediff(year,'2009-2-25','2009-7-28' ) , month=datediff(month,'2009-2-25','2009-7-28')

select month=datediff(month,'2009-2-25','2009-7-28')

select day=datediff(day,'2009-2-25','2009-7-28')