簡體   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