开发者

Months difference between dates [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Difference in months

Hi all:

how can We calculate the months difference between two dates using LI开发者_如何转开发NQ ? I can find days difference using:

(p.Account.StateChangeDate.Date - DateTime.Now.Date).Days < 4

but there is no option for months.

Please suggest.


Try calculating the difference in months between two dates


Pick the algorithm you want to use from that question.

Then, if you are using Linq2Sql, then almost all of those will get mapped back to the database as DATEPART type operations (I think).

Alternatively you could do the calc in SQL using a function like in http://www.sqlmag.com/article/sql-server/calculating-month-difference.aspx - and could then expose that to Linq2Sql as a function


This should calculate the months for you but it is irregular because some months have 30 days and february, for example, can have 28 or 29.

int months = MaxDate.Subtract(MinDate).Days / 30;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜