gpt4 book ai didi

c# - "semi-monthly"加多少天

转载 作者:太空狗 更新时间:2023-10-29 21:19:02 27 4
gpt4 key购买 nike

我有一个名为 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
}

基于 NumberOfPaymentsPaymentFrequencyFirstPaymentDate(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 时,我应该使用多少天/月?如果不知道中间每个月的确切天数,这甚至有可能吗?或者这真的很简单,我刚刚喝完咖啡因,只见树木不见森林 :)

最佳答案

对于半月,如果您的第一笔付款也始终是当月的第一笔付款(即,从 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 天,因此是第一次付款日期。对于第 2 次付款,这将增加 0 个月(整数除法,剩余部分被剔除)和 16 日的 15 天。对于第 3 次付款,这将增加 1 个月 (1/3) 和下个月 1 日的 0 天,依此类推。

这是假设 FirstPaymentDate 将在某个给定月份的 1 号。如果您想将 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

关于c# - "semi-monthly"加多少天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8481517/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com