C# C中考虑30天月的天数计算#

C# C中考虑30天月的天数计算#,c#,.net,date,calendar,C#,.net,Date,Calendar,我正试图写一个程序来计算每个月的账单金额。考虑模型。i、 e用于开具发票的金额基于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

我正试图写一个程序来计算每个月的账单金额。考虑模型。i、 e用于开具发票的金额基于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

使用下面的代码,我能够得到预期的结果,除了突出显示的情况。我如何设置在月底之前进行计算的限制?我的意思是,如果开始日期是10月5日,结束日期是10月6日,那么根据天数(22)计算到31月5日


我不想计算开始日期和结束日期之间的天数。我想计算从开始日期到该月底的总成本。如果客户在5月10日订阅了一款产品,并在6月10日结束订阅。费用按月计费。因此,客户通过计算从5月10日到5月31日的成本来计费。其余几天的账单发生在六月周期,依此类推,我运行了你的代码,它实际上说是31天。 与:


如上面的评论所述,如果您试图从10月5日到31月5日找到它也是正确的,它的22天

如果我了解您的要求,您只想计算开始日期月份的金额。那么,以下内容也应该更简单、更简洁:

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天。

您可以使用这种方法计算两个日期之间的总天数

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;

您应该指定25是要支付的金额,所以问题是,当开始日期为05/10,结束日期为06/10时,您没有获得正确的天数?我不知道。如果结束日期在另一个月,您希望计算到开始日期的当月结束?这里的问题一定是因为开始日期和结束日期是相同的。您的
code
工作与预期的一样。我不想计算开始日期和结束日期之间的天数。我想计算从开始日期到该月底的总成本。如果客户在5月10日订阅了一款产品,并在6月10日结束订阅。费用按月计费。因此,客户通过计算从5月10日到5月31日的成本来计费。其余日期的账单发生在6月周期,依此类推。好的,我现在明白了,请更新您的问题以帮助我们解决此问题函数提供两个日期之间的日期差,将整个月视为30天
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;
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;
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;
}