【问题标题】:Calculation of number of days considering 30 day month in C#在 C# 中计算考虑每月 30 天的天数
【发布时间】:2017-06-12 13:31:47
【问题描述】:

我正在尝试编写一个程序来计算每个月的账单金额。考虑30/360 模型。即用于开票的金额基于 30 天的月份,无论该月是 28 天、30 天还是 31 天。我正在计算开始日期(成员注册日期)到结束日期(成员退出日期)之间的天数。每天的费用是 25 美元这是一个小桌子,例如:

**日期为 (MM/DD)。

开始日期 |结束日期 |天数 |总金额

05/01 | 05/31 | 30 | ($25/30)*30=25

05/10 | 05/31 | 22 | ($25/30)*22=18.33

05/10 | 06/10 | 22 | ($25/30)*22=18.33

05/01 | 05/12 | 12 | ($25/30)*12=10

05/06 | 05/15 | 10 | ($25/30)*10=8.33

第 3 行的预期结果 = ($25/30)*22=18.33

当前结果 = ($25/30)*31=25.83

使用下面的代码,我可以得到预期的结果,除了突出显示的情况。如何限制在月底之前进行计算?我的意思是如果开始日期是 05/10,结束日期是 06/10,则根据天数 (22) 计算到 05/31。

    public static void Main(string[] args)
    {
        var start = new DateTime(2016, 05, 10);
        var finish = new DateTime(2016, 05, 28);

        var date1 = start.Day == 31 ? 30 : start.Day;
        var date2 = finish.Day == 31 && (start.Day == 30 || start.Day == 31) ? 30 : finish.Day;

        var actualDaysDiff = (finish - start).TotalDays;
        int newDaysDiff = ((360 * (finish.Year - start.Year)) + (30 * (finish.Month - start.Month)) + (date2 - date1))+1; 

        Console.WriteLine("The number of days are "+ newDaysDiff);

        float invoiceAmount = 0;
        invoiceAmount = (float)(25.0/30)*newDaysDiff;

        Console.WriteLine("Total Invoice for this month : "+ invoiceAmount);

    }    

例如:我不想计算开始日期和结束日期之间的天数。我想计算从开始日期到该特定月份结束的总成本。如果客户在 5 月 10 日订阅产品并在 6 月 10 日结束订阅。费用按月计费。因此,客户通过计算从 5 月 10 日到 5 月 31 日的成本来计费。其余天的计费发生在六月周期等等

【问题讨论】:

  • 您应该指定 25 是要支付的金额
  • 所以问题是当开始是 05/10 和结束是 06/10 时,您没有得到正确的天数?
  • 我不明白。如果结束日期在另一个月份,您想计算到开始日期的月底?
  • 这里的问题一定是因为startday和endday是一样的
  • 您的code 工作正常

标签: c# .net date calendar


【解决方案1】:
public static int GetDays(DateTime start, DateTime end) {
    int newDaysDiff = ((360 * (end.Year - start.Year)) + (30 * ((end.Month - start.Month) == 0 ? 0 : (end.Month - start.Month) - 1)));
    int j = 0;
    if (start.Month == end.Month && start.Year == end.Year) {
        if (start.Day == 1 && end.Day == 31) {
            j = (end.Day - start.Day);
        }
        else {
            j = (end.Day - start.Day) + 1;
        }
    }
    else {
        if (start.Day == 1) {
            j = j + 30;
        }
        else {
            j = j + DateTime.DaysInMonth(start.Year, start.Month) - start.Day + 1;
        }
        if (end.Day == 30 || end.Day == 31) {
            j = j + 30;
        }
        else {
            j = j + end.Day;
        }
    }
    newDaysDiff = newDaysDiff + j;
    return newDaysDiff;
}

【讨论】:

  • 函数提供两个日期之间的日期差异,将整月视为 30 天
【解决方案2】:

如果我了解您的要求,您只想计算开始日期月份的金额。然后以下应该可以工作,这也更简单,更简洁:

int startDateDays = DateTime.DaysInMonth(start.Year, start.Month);
if (finish.Month != start.Month || finish.Year != start.Year)
    finish = new DateTime(start.Year, start.Month, startDateDays);
int newDaysDiff = (finish - start).Days + 1;
if (newDaysDiff >= startDateDays)
    newDaysDiff = 30;

float invoiceAmount = (float)(25.0 / 30) * newDaysDiff;

使用这种方法,您突出显示的整月行将被视为 22 天。

【讨论】:

    【解决方案3】:

    我运行了您的代码,它实际上是 31 天。 与:

    var start = new DateTime(2016, 05, 10);     
    var finish = new DateTime(2016, 06, 10);
    

    http://rextester.com/IKE75462

    如上面的 cmets 中所述,如果您试图找到从 5/10 到 5/31 的时间,那也是正确的,它是 22 天

    【讨论】:

    • Gabri - 我不想计算开始日期和结束日期之间的天数。我想计算从开始日期到该特定月份结束的总成本。如果客户在 5 月 10 日订阅产品并在 6 月 10 日结束订阅。费用按月计费。因此,客户通过计算从 5 月 10 日到 5 月 31 日的成本来计费。其余天的计费发生在六月周期等等。
    • 好的,我明白了,请更新您的问题以帮助我们解决此问题
    【解决方案4】:

    您可以使用它来计算两个日期之间的总天数

    var start = new DateTime(2016, 05, 01);
    var finish = new DateTime(2016, 05, 31);
    
    var daysBetween = (finish - start).TotalDays + 1;
    daysBetween = daysBetween > 30 ? 30 : daysBetween;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      • 1970-01-01
      相关资源
      最近更新 更多