我想为每个月创建一个连续的付费期。假设我插入了最后一个月的开始日期:01-01-2012 12:00:00 enddate:30:01:2012 11:59:00交易日期:31:01:2012 12:00:00状态:打开。现在我想要基于以上条目
的以下结果 2. start date : 01-02-2012 12:00:00 enddate : 28:02:2012 11:59:00 Transaction date : 29:02:2012 12:00:00 and status : CLOSED
3. start date : 01-03-2012 12:00:00 enddate : 30:03:2012 11:59:00 Transaction date : 31:03:2012 12:00:00 and status : OPEN
并且第一个状态OPEN变为CLOSE。这个过程会根据第一次进入开始,结束和交易日期自动连续,请帮我做这个...
答案 0 :(得分:2)
请尝试以下代码
public class Result
{
private int _daysInMonth;
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public DateTime TransactionDate { get; set; }
public string Status { get; set; }
public ResultCollection Generate(DateTime fromdate, DateTime enddate)
{
var collection = new ResultCollection();
for (; fromdate < enddate; fromdate = fromdate.AddMonths(1))
{
_daysInMonth = DateTime.DaysInMonth(fromdate.Year, fromdate.Month);
var res = new Result
{
StartDate = fromdate,
EndDate = fromdate.AddDays(_daysInMonth - 1).AddMinutes(-1),
TransactionDate = fromdate.AddDays(_daysInMonth - 1),
Status = "Close"
};
collection.Add(res);
}
collection.ElementAt(collection.Count-1).Status = "Open";
return collection;
}
}
public class ResultCollection : System.Collections.ObjectModel.Collection<Result>
{
}
使用
调用该功能 DateTime start = new DateTime(2012, 01, 01);
DateTime end = new DateTime(2012, 05, 01);
GridView1.DataSource = new Result().Generate(start, end);
GridView1.DataBind();
我希望这会有所帮助!!!