如何计算日期范围内有多少夜晚? [英] How can I calculate how many nights are there in a date range?

查看:123
本文介绍了如何计算日期范围内有多少夜晚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



最好的方式是什么?

我需要从入住日期和退房日期计算住宿的夜晚数量。 p>

ie:如果我有

 入住:12/11/2009 15 :00 hs 
结帐:14/11/2009 12:00 hs

执行(Checkout - Checkin).Days 会给我1晚而不是2



我在想添加一个简单的如果检查时间(如果检入时间大于结帐时间),并添加缺少的夜晚,但也许有一个更好的算法

解决方案

DateTime有一个属性可以在午夜返回DateTime的Date部分。您可以使用此部分获得夜晚,因为每天的所有时间段将映射到当天的同一时间。

 (Checkout 。日期 -  Checkin.Date).Days 


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 instead 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

这篇关于如何计算日期范围内有多少夜晚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆