开发者

How can I calculate how many nights are there in a date range?

开发者 https://www.devze.com 2022-12-10 05:40 出处:网络
I need to calculate the quantity of nights (stay at a hotel) from the checkin and checkout dates. What is the best way to do it?

I need to calculate the quantity of nights (stay at a hotel) from the checkin and checkout dates.

What is the best way to do it?

ie: If I have

Checkin:  12/11/2009 15:00 hs  
Checkout:  14/11/2009 12:00 hs

Doing (Checkout - Checkin).Days would give me 1 night ins开发者_JAVA百科tead of 2

I'm thinking of adding a simple if to check the hours (if checkin-time is greater than checkout-time) and add the missing night, but maybe there's a better "algorithm"


DateTime has a property that returns the Date part which is the DateTime in midnight. You can use this part to get the nights since all parts of day will be mapped to the same time at day:

(Checkout.Date - Checkin.Date).Days
0

精彩评论

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