我有一个名为PaymentFrequency
的枚举类型,其值表示每年有多少付款...
所以我有
public enum PaymentFrequency
{
None = 0,
Annually = 1,
SemiAnnually = 2,
EveryFourthMonth = 3,
Quarterly = 4,
BiMonthly = 6,
Monthly = 12,
EveryFourthWeek = 13,
SemiMonthly = 24,
BiWeekly = 26,
Weekly = 52
}
根据NumberOfPayments
,PaymentFrequency
和FirstPaymentDate
(类型为DateTimeOffset),我想计算LastPaymentDate
。但是,我有一个问题,想知道在SemiMonthly的情况下要添加多少时间单位(天,月)......
switch (paymentFrequency)
{
// add years...
case PaymentFrequency.Annually:
LastPaymentDate = FirstPaymentDate.AddYears(NumberOfPayments - 1);
break;
// add months...
case PaymentFrequency.SemiAnnually:
LastPaymentDate = FirstPaymentDate.AddMonths((NumberOfPayments - 1) * 6); // 6 months
break;
case PaymentFrequency.EveryFourthMonth:
LastPaymentDate = FirstPaymentDate.AddMonths((NumberOfPayments - 1) * 4); // 4 months
break;
case PaymentFrequency.Quarterly:
LastPaymentDate = FirstPaymentDate.AddMonths((NumberOfPayments - 1) * 3); // 3 months
break;
case PaymentFrequency.BiMonthly:
LastPaymentDate = FirstPaymentDate.AddMonths((NumberOfPayments - 1) * 2); // 2 months
break;
case PaymentFrequency.Monthly:
LastPaymentDate = FirstPaymentDate.AddMonths(NumberOfPayments - 1);
break;
// add days...
case PaymentFrequency.EveryFourthWeek:
LastPaymentDate = FirstPaymentDate.AddDays((NumberOfPayments - 1) * 4 * 7); // 4 weeks (1 week = 7 days)
break;
case PaymentFrequency.SemiMonthly:
// NOTE: how many days in semi month? AddMonths (0.5) does not work :)
LastPaymentDate = FirstPaymentDate.AddMonths((NumberOfPayments - 1) * 0.5); // 2 weeks (1 week = 7 days)
break;
case PaymentFrequency.BiWeekly:
LastPaymentDate = FirstPaymentDate.AddDays((NumberOfPayments - 1) * 2 * 7); // 2 weeks (1 week = 7 days)
break;
case PaymentFrequency.Weekly:
LastPaymentDate = FirstPaymentDate.AddDays((NumberOfPayments - 1) * 7); // 1 week (1 week = 7 days)
break;
case PaymentFrequency.None:
default:
throw new ArgumentException("Payment frequency is not initialized to valid value!", "paymentFrequency");
}
那么,使用SemiMonthly时我应该使用多少天/几个月? 如果不知道每个月的确切天数,这是否可能? 或者这真的很简单,我刚用完了咖啡因而且我没有看到森林的树木:)
答案 0 :(得分:7)
对于半月刊,如果您的第一笔付款始终是该月的第一笔付款(也就是说,从第1日到第13日的任何时间,从第13次开始是有问题的,如评论中所述),您可以这样做如下:
// assuming first payment will be 1st of month, add month for every 2 payments
// num payments / 2 (int division, remainder is chucked)
// then add 15 days if this is even payment of the month
LastPaymentDate = FirstPaymentDate.AddMonths((NumberOfPayments - 1) / 2)
.AddDays((NumberOfPayments % 2) == 0 ? 15 : 0);
因此,对于第一次付款,这将添加0个月和0天,因此是第一个付款日期。对于第二次付款,这将增加0个月(int dividision,其余被清除)和15个月的16个月。对于第三次付款,这将增加下个月1日的1个月(1/3)和0天等等。
这假设FirstPaymentDate将在某个月的第一天。如果你想让第16个成为开始日期等,你可能会看到从这里去的地方等。
有意义吗?
所以说明一下,如果我们有:
DateTime LastPaymentDate, FirstPaymentDate = new DateTime(2011, 12, 5);
for(int numOfPayments=1; numOfPayments<=24; numOfPayments++)
{
LastPaymentDate = FirstPaymentDate.AddMonths((numOfPayments - 1) / 2)
.AddDays((numOfPayments % 2) == 0 ? 15 : 0);
Console.WriteLine(LastPaymentDate);
}
这个循环会给我们:
12/5/2011 12:00:00 AM
12/20/2011 12:00:00 AM
1/5/2012 12:00:00 AM
// etc...
10/20/2012 12:00:00 AM
11/5/2012 12:00:00 AM
11/20/2012 12:00:00 AM
答案 1 :(得分:1)
因为月份的长度各不相同,所以您不能只添加预定义的数字。你必须知道你正在处理哪个月,并从那里开始。
如果你知道一个月的第一个和第十六个是截止日期,那么最后一笔付款是12月16日(假设你正在计算一个日历年)。
答案 2 :(得分:1)
半月付款的基本配对是:
偷看并选择
答案 3 :(得分:0)
我最近遇到了同样的问题,但我需要允许任何日期输入。这有点乱,需要重构,但这是我到目前为止所提出的。二月有一些我不得不破解的问题。
Date returnDate;
if (numberOfPayments % 2 == 0)
{
returnDate = date.AddMonths(numberOfPayments / 2);
if (date.Day == DateTime.DaysInMonth(date.Year, date.Month))//Last day of the month adjustment
{
returnDate = new Date(returnDate.Year, returnDate.Month, DateTime.DaysInMonth(returnDate.Year, returnDate.Month));
}
}
else
{
returnDate = date.Day <= 15 ? date.AddDays(15).AddMonths((numberOfPayments - 1) / 2) : date.AddDays(-15).AddMonths((numberOfPayments + 1) / 2);
if (date.Day == DateTime.DaysInMonth(date.Year, date.Month))//Last day of the month adjustment
{
returnDate = new Date(returnDate.Year, returnDate.Month, 15);
}
else if (date.Month == 2 && date.Day == 14)
{
returnDate = returnDate.AddMonths(-1);
returnDate = new Date(returnDate.Year, returnDate.Month, returnDate.Month == 2 ? 28 : 29);
}
else if (date.Month == 2 && date.Day == 15)
{
returnDate = returnDate.AddMonths(-1);
returnDate = new Date(returnDate.Year, returnDateMonth, DateTime.DaysInMonth(returnDate.Year, returnDate.Month));
}
}
return returnDate;