How do c and sql get the time interval method

  • 2020-05-27 06:56:44
  • OfStack

The use of the TimeSpan

TimeSpan is used to represent an instance of a time period. The difference between two times can form an instance of TimeSpan. Now I will briefly introduce the following important USES:

a starts with a few methods

TimeSpan.Minutes (the difference in minutes obtained when other times, such as days, hours and seconds, are all one), other Hours, Second1

DateTime. Tick: is a timing cycle, representing 1 nanosecond, or 1 millionth of a second, so Ticks here represents the total difference in time cycles, namely: 9 * 24 * 3600 * 10000000 + 23 * 3600 * 10000000 + 59 * 60 * 10000000 + 59 * 10000000 = 8639990000000. 3,600 is the number of seconds in an hour

TimeSpan.TotalDays: the number of days that differ between two time periods; other TotalHours,TotalMinutes,TotalSeconds 1 like


b difference between two times


string time1 = "2010-5-26 8:10:00";
string time2 = "2010-5-26 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: represents the difference between two time periods

diff: represents the number of minutes between two times. 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,'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')


Related articles: