The built-in method for calculating the date interval is also convenient. By introducing the namespace system. Data. LINQ. sqlclient, you can use various date interval calculation methods provided by sqlmethods to calculate the date difference.
System. Data. LINQ. sqlclient. sqlmethods. datediffday (starttime, endtime), the returned value is int type.
. Net also provides us with the date interval calculation method, timespan and substract. Now, for a test, we have the following date interval calculation code:
Public static int substract (datetime? Starttime, datetime? Endtime) <br/>{< br/> datetime _ starttime = convert. todatetime (starttime); <br/> datetime _ endtime = convert. todatetime (endtime); <br/> timespan tstart = new timespan (_ starttime. ticks); <br/> timespan torderused; <br/> timespan tend = new timespan (_ endtime. ticks); <br/> torderused = tend. subtract (tstart ). duration (); <br/> int usedays = torderused. days; <br/> return usedays; <br/>}
The input parameter starttime: 11:54:20, endtime: 15:34:41;
The returned value is uerdays: 31.
Query and computation using the LINQ method:
System. Data. LINQ. sqlclient. sqlmethods. datediffday (starttime, endtime)
The returned result is 32.
In order to keep the results of the two methods consistent, the date format can be converted locally and then calculated without the Group's full day:Public static int substract (datetime? Starttime, datetime? Endtime) <br/>{< br/> datetime _ starttime = convert. todatetime (starttime); <br/> datetime _ endtime = convert. todatetime (endtime); <br/> timespan tstart = new timespan (_ starttime. ticks); <br/> timespan torderused; <br/> timespan tend = new timespan (_ endtime. ticks); <br/> torderused = tend. subtract (tstart ). duration (); <br/> int usedays = torderused. days; <br/> return usedays; <br/>}
Returned result: userdays = 32.
In this way, the data of the two versions is consistent.