C# 日期计算算法
我之前已经发布了这个问题。但是我不够具体,所以我试图在这篇文章中更好地解释它 我目前正在编写一个创建发票的小程序。发票应根据物品的储存时间进行计算,例如,C# 日期计算算法,c#,date,datetime,date-arithmetic,C#,Date,Datetime,Date Arithmetic,我之前已经发布了这个问题。但是我不够具体,所以我试图在这篇文章中更好地解释它 我目前正在编写一个创建发票的小程序。发票应根据物品的储存时间进行计算,例如,costPerDay*numberofdays储存 发票按月创建,例如,InvoiceDate=31/05/2013。 从数据库中提取项目开始日期和结束日期(存储中) 因此,我: 变量: DateTime StartInStorageDate; DateTime EndOfStorageDate; DateTime InvoiceDat
costPerDay*numberofdays储存
发票按月创建,例如,InvoiceDate=31/05/2013。
从数据库中提取项目开始日期和结束日期(存储中)
因此,我:
变量:
DateTime StartInStorageDate;
DateTime EndOfStorageDate;
DateTime InvoiceDate; //(always last date of month)
规则
- 储存的前五天应始终免费
- 天数的计算应仅针对给定的发票日期进行
- 如果存在发票日期之前的日期,则应采用天数 考虑到,以计算“自由日”。如果前一个月的月末有5天或5天以上,我们必须假设这些天数已经计算过,因此在发票月份不考虑这些天数
- 例1:
DateTime StartInStorageDate=2013年4月7日代码>
DateTime EndOfStorageDate=2013年6月8日代码>
DateTime InvoiceDate=31/05/2013代码> 天数的计算=31天(因为发票日期比发票日期早,超过五天,“自由天数”已被取消 当月减去)
- 例2:
DateTime StartInStorageDate=28/04/2013
DateTime EndOfStorageDate=08/06/2013
天数的计算=(总共11天-5“自由日”)=6天 (因为发票日期在日期之前,没有5天的时间。) 月底,我们必须将这些天添加到发票月,并且 从总数中减去5“自由日”)DateTime InvoiceDate=31/05/2013
- 例3:
DateTime StartInStorageDate=28/04/2013
DateTime EndOfStorageDate=08/05/2013
天数的计算=0(因为invoiceDate的天数不超过5天,并且没有前一个月)DateTime InvoiceDate=31/04/2013
谢谢。它只需要几个简单的操作:
DateTime endDate = Min(invoiceDate, endOfStorageDate ); // Min() is pseudo code
int daysInStorage = (endDate - StartInStorageDate).Days;
daysInStorage -= 5;
if (daysInStorage < 0) daysInStorage = 0;
DateTime endDate=Min(invoiceDate,endOfStorageDate);//Min()是伪代码
int daysinsstorage=(endDate-StartInStorageDate).Days;
储存天数-=5天;
如果(daysInStorage<0)daysInStorage=0;
您需要计算两个日期:发票期的开始和结束日期:
DateTime invoiceStart = StartInStorageDate.AddDays(5);
DateTime invoiceEnd = InvoiceDate < EndOfStorageDate ? InvoiceDate : EndOfStorageDate;
double billedDays = Math.Max(0, (invoiceEnd - invoiceStart).TotalDays);
DateTime invoiceStart=StartInStorageDate.AddDays(5);
DateTime invoiceEnd=InvoiceDate
不太确定您想要做什么,但看起来您应该看看Min
我会这样做:DateTime endDate=new DateTime(Math.Min(invoiceDate.Ticks,endOfStorageDate.Ticks))代码>或者干脆DateTime endDate=invoiceDate感谢您的回答和宝贵的时间。非常感谢。