简体   繁体   English

使用NodaTime计算包容性天数

[英]use NodaTime to calculate an inclusive days period

So for example if I have the following code: 例如,如果我有以下代码:

var nodaStart = new LocalDate(2012, 5, 1);
var nodaEnd = new LocalDate(2012,5,2);
var daysBetween = Period.Between(nodaStart, nodaEnd,PeriodUnits.Day);

Then daysBetween.Days == 1 然后daysBetween.Days == 1

However, the range I calculate needs to count that as 2 days. 但是,我计算的范围需要计算为2天。 ie it needs to be inclusive of the start and end date. 即它需要包括开始和结束日期。

The actual method can take and start and end date (that are no more than a year apart) and needs to calculate the number of days. 实际的方法可以采取开始和结束日期(相隔不超过一年),并需要计算天数。 If there are more than 31 days then the remainder are returned as a number of whole weeks. 如果超过31天,则剩余的数量将返回整周。

I have that logic working fine but because the count is exclusive I'm one day out. 我有那种逻辑工作正常,但因为计数是独家我有一天出去了。

I guess I can just do startDate.addDays(-1) before I create nodaStart from it but I wonder if there's a more elegant / pretty way to have noda return the Period. 我想我可以在创建nodaStart之前做startDate.addDays(-1) ,但我想知道是否有更优雅/漂亮的方式让noda返回Period。

Thanks 谢谢


UPDATE: I've had a read of the source code for the Period class and the + operator is overloaded so I can add 更新:我已经阅读了Period的源代码 ,并且+运算符被重载,所以我可以添加

daysBetween += Period.FromDays(1);

(Sorry it's taken me so long to answer this - I hadn't seen it before.) (对不起,我花了很长时间才回答这个问题 - 我之前没见过。)

Any of: 任何

  • Adding a day to the end before calculating (this is the most logical approach, IMO - as Roger says, you want the start of the next day, effectively) 在计算之前将一天添加到最后(这是最合乎逻辑的方法,IMO - 正如Roger所说,你希望第二天开始,有效)
  • Subtracting a day from the start before calculating 在计算之前从开始减去一天
  • Adding 1 to the number of days you get out of the end 将1加到你结束的天数

should be fine. 应该没事。 I don't think Noda Time will change to make this any simpler. 我认为Noda Time不会改变这一点。 Between is a sort of "fuzzy around units" version of a subtraction operator - and you won't find many subtraction operators where 2 - 1 is 2. Between是一种减法运算符的“模糊周围单位”版本 - 你不会发现许多减法运算符,其中2 - 1是2。

For "fuzzy" brained humans, we may consider a period of days to be inclusive of start and end date if it identifies a single day, week, month, etc (cf. whole multiple of), so you could code it: 对于“模糊”的脑子,我们可以考虑一段时间来包括开始和结束日期,如果它标识一天,一周,一个月等(参见整数倍),所以你可以编码它:

var start = new NodaTime.LocalDateTime(s.Year, s.Month, s.Day, s.Hour, s.Minute);
var end = new NodaTime.LocalDateTime(e.Year, e.Month, e.Day, e.Hour, e.Minute);

NodaTime.Period periodInclusive = NodaTime.Period.Between(start, end.PlusDays(1), NodaTime.PeriodUnits.AllDateUnits);
NodaTime.Period period =  NodaTime.Period.Between(start, end, NodaTime.PeriodUnits.AllDateUnits);
bool isInclusivePeriod = periodInclusive.Days + periodInclusive.Weeks + periodInclusive.Months + periodInclusive.Years < 
                         period.Days + period.Weeks + period.Months + period.Years;

period = isInclusivePeriod ? periodInclusive : period;
// do stuff with period here....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM