Differences between the. NET date calculation function and the LINQ date calculation function

Source: Internet
Author: User

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.

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.