繁体   English   中英

C#计算日期

[英]C# Calculating Dates

我正在寻找一些协助来计算新计划系统的到期日期。

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

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

我们提供13种付款频率选项:

  1. 蒙特利-最后工作日
  2. 每月-同一日期,例如20日,25日
  3. 每月-第1个周一/周二/周三/周四/周五
  4. 每月-第2个周一/周二/周三/周四/周五
  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;
}

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

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;
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM