所以我有
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时我应该使用多少天/月?
如果不知道每个月的确切天数,这是否可能?
或者这真的很简单,我刚用完了咖啡因而且我没有看到森林里的树木:)
// 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/3)和下个月1日的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 AM12/20/2011 12:00:00 AM1/5/2012 12:00:00 AM// etc...10/20/2012 12:00:00 AM11/5/2012 12:00:00 AM11/20/2012 12:00:00 AM总结
以上是内存溢出为你收集整理的c# – 为“半月刊”添加多少天全部内容,希望文章能够帮你解决c# – 为“半月刊”添加多少天所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)