【发布时间】:2011-07-20 15:42:42
【问题描述】:
我为我的新计费系统编写了一些代码。目的是在每个月的同一天向客户开具账单。 (不是每月的第一天或最后一天)
static bool NeedToBill(DateTime planLastBilled, DateTime cycleDate)
{
// is today the same date as the cycleDate AND is was the planLastBilled not the same day as today?
if (DateTime.UtcNow.Day.Equals(cycleDate.Day) && !DateTime.UtcNow.Day.Equals(planLastBilled))
return true;
else
return false;
}
两个陷阱是:
- 如果他的 cycleDate.Day 是 31 并且当月只有 29 天
- cycleDate 是 2012 年 2 月 29 日 - 他只会在闰年计费
这里有通用的最佳实践吗?
所以看起来有很多事情要检查
- 此帐户本月是否已计费?
- 当月是否存在循环日
- 是大于或等于当前日期的循环日(如果 交易在前一天失败)
谢谢!
【问题讨论】: