【问题标题】:C# Calculating DatesC# 计算日期
【发布时间】:2012-12-12 13:43:32
【问题描述】:

我正在寻求一些帮助来计算新调度系统的截止日期。

目前的时间表是基于每月或每周付款,从定义的日期开始。

在新的时间表中,我们希望这基于客户的付款频率,从定义的日期开始。

我们有 13 种支付频率选项:

  1. 每月 - 最后一个工作日
  2. 每月 - 同一日期,例如20日、25日
  3. 每月 - 第一个周一/周二/周三/周四/周五
  4. 每月 - 第二个周一/周二/周三/周四/周五
  5. 每月 - 第 3 个周一/周二/周三/周四/周五
  6. 每月 - 上周一/周二/周三/周四/周五
  7. 每周 - 星期一
  8. 每周 - 周二
  9. 每周 - 周三
  10. 每周 - 星期四
  11. 每周 - 周五
  12. 每周 4 次
  13. 双周刊

根据传入的付款频率以及付款次数和应付余额,我需要生成一个新的时间表。

第一次付款是直截了当的,因为它是在传递的日期,时间表中的其他日期需要从该日期计算(取决于付款频率)。

我还需要确保安排的日期是在工作日(周一至周五),并且不会在公共/银行假日降落 - 在这种情况下,我将恢复到下一个有效的工作日。

到目前为止我的方法如下 - 我需要帮助的区域评论:

// 计算下一个付款日期

public IList<ScheduledInstalment> GenerateSchedule(int agreementID, int paymentCount, 
    PayFrequency frequency, double balance, DateTime firstPaymentDate)
{
    IList<ScheduledInstalment> schedule = new List<ScheduledInstalment>();

    PaymentCalculation calc = GetPaymentCalculation(frequency, firstPaymentDate);

    double regularInstalment = Math.Round(balance / paymentCount, 1);
    double finalInstalment = Math.Round(((regularInstalment * (paymentCount - 1)) - balance), 2);

    for (int i = 0; i <= paymentCount; i++)
    {
        ScheduledInstalment s = new ScheduledInstalment();

        s.AgreementID = agreementID;

        if (i == 0)
        {
            // First Payment
            s.DueDate = firstPaymentDate;
            s.AmountDue = regularInstalment;
        }
        else 

            // Calculate next payment date

            if (i < paymentCount)
            {
                // Regular Payment
                s.AmountDue = regularInstalment;
            }
            else
            {
                // Final Payment
                s.AmountDue = finalInstalment;
            }

        schedule.Add(s);
    }

    return schedule;
}

【问题讨论】:

  • 你有什么问题?...或者,有什么问题?
  • 我认为他需要帮助解决elseif(i &lt; paymentCount) 之间的逻辑...
  • 没错,迈克,我需要帮助来确定每个循环中的截止日期。
  • 缺乏细节的问题通常是问题所在,但在这种情况下,问题必须非常详细。尝试将您的代码简化为您需要的方法。考虑您需要一种使用各种参数来计算下一个到期日的方法,我们只需要知道该方法以及我们有哪些参数。您在该方法中尝试过的内容。不是你必须做的进入方法本身:)
  • 嗨,卢克,我已经包含了我需要代码帮助的位置。我还提供了 2 种其他方法来帮助解决这个问题。

标签: c# date date-arithmetic


【解决方案1】:

好的,我设法得到了一些似乎可行的方法,下面是我的方法:

public DateTime GetNextRepaymentDate(DateTime BaseDate, int instalmentCount, PaymentCalculation calc)
            {
                DateTime dueDate = new DateTime();

                switch (calc.Interval)
                {
                    case DateInterval.Month:

                        dueDate = BaseDate.AddMonths((instalmentCount) * calc.Number);

                        if (!string.IsNullOrEmpty(calc.OtherCriteria))
                        {
                            if (calc.OtherCriteria == "Last")
                            {
                                int lastDay = DateTime.DaysInMonth(dueDate.Year, dueDate.Month);
                                dueDate = Convert.ToDateTime(string.Format("{0}/{1}/{2}", lastDay, dueDate.Month, dueDate.Year));
                            }
                            else
                            {
                                int fixedDate = Convert.ToInt32(calc.OtherCriteria);

                                if (dueDate.Day != fixedDate)
                                {
                                    if (dueDate.Day > fixedDate)
                                    {
                                        while (dueDate.Day != fixedDate)
                                        {
                                            dueDate = dueDate.AddDays(-1);
                                        }
                                    }
                                    else
                                    {
                                        while (dueDate.Day != fixedDate)
                                        {
                                            dueDate = dueDate.AddDays(1);
                                        }
                                    }
                                }
                            }
                        }

                        break;

                    case DateInterval.WeekOfYear:

                        dueDate = BaseDate.AddDays((instalmentCount) * (calc.Number * 7));

                        if (calc.FixedDay != null)
                        {
                            while (dueDate.DayOfWeek != calc.FixedDay)
                            {
                                dueDate = dueDate.AddDays(-1);
                            }
                        }

                        break;
                }

                while (!PaymentIsAllowedOnDate(dueDate) == true)
                {
                    dueDate = dueDate.AddDays(-1);
                }

                return dueDate;
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多