我有两个日期,如2017-10-05和2018-02-20,我的间隔结果应为:
2017-10-05
2017-10-31
2017-11-30
2017-12-31
2018-01-31
2018-02-20
2018-02-28
使用DateTime对象在C#中执行此操作的任何建议吗?
更新#1
我首先提到的问题是现金流量管理的真实情况,因此我不得不更改日期规格以计算利息,差异和文明。这个代码我用于此目的:
foreach (DataRow dr in DTFlussi.Rows)
{
DateTime d1 = Convert.ToDateTime(DTFlussi.Rows[n]["data_scadenza"]).Date;
DateTime d2 = Convert.ToDateTime(DTFlussi.Rows[n + 1]["data_scadenza"]).Date;
int[] date = RecuperoDate(d1, d2);
if (saldoConto > 0)
InteressiAttivi(saldoConto, date, DTOneri, d1, d2);
else if (saldoConto <= 0 && saldoConto >= (fidoConto * -1))
{
//esempio di interessi FIDO al 2%
InteressiFido(saldoConto, date, tassoFIDO, DTOneri, d1, d2);
}
else if (saldoConto < 0 && saldoConto < (fidoConto * -1))
{
//esempio con interessi FIDO al 2% e interessi EXTRAFIDO al 4%
InteressiExtraFido(saldoConto, fidoConto, date, tassoFIDO, tassoEXTRAFIDO, DTOneri, d1, d2);
}
catch (Exception)
{
}
n++;
}
RecuperoDate的代码是:
public int[] RecuperoDate(DateTime d1, DateTime d2)
{
int[] diff = new int[2];
//Gestione anno bisestile Febbrario
int giorniFebbraio = 0;
if (DateTime.IsLeapYear(d1.Year))
giorniFebbraio = 29;
else
giorniFebbraio = 28;
try
{
#region MESE 1
if (d1.Date >= new DateTime(d1.Year, 1, 1) && d1.Date <= new DateTime(d1.Year, 1, 31) && d2.Date >= new DateTime(d1.Year, 1, 31))
{
DateTime Mese1 = new DateTime(d1.Year, 1, 31).Date;
diff[0] = (int)(Mese1 - d1).TotalDays;
diff[1] = (int)(d2 - Mese1).TotalDays;
}
#endregion
//NOTE: incomplete code because is the same thing for the other months (February - December)
答案 0 :(得分:0)
创建一个数组。
DateTime[] dateArray = {
DateTime.Parse("2017-10-05"),
DateTime.Parse("2017-10-31"),
DateTime.Parse("2017-11-30"),
DateTime.Parse("2017-12-31"),
DateTime.Parse("2018-02-20"),
DateTime.Parse("2018-02-28")
};
答案 1 :(得分:0)
我认为您所寻找的是每个月的最新一天。 DateTime
结构提供静态DaysInMonth
方法,将年份和月份作为参数。
从那里开始,您可以从开始日期到结束日期进行迭代并获取日期。
此代码会将所需的日期打印到控制台,但格式不是您要求的格式:
DateTime start = new DateTime(2017,10,05);
DateTime end = new DateTime(2018,2,20);
DateTime current = start;
Console.WriteLine(start.ToShortDateString());
int startLastDay = DateTime.DaysInMonth(start.Year, start.Month);
if(startLastDay != start.Day)
Console.WriteLine(new DateTime(start.Year, start.Month, startLastDay).ToShortDateString());
while(current < end)
{
current = current.AddMonths(1);
if(current > end)
Console.WriteLine(end.ToShortDateString());
int lastDay = DateTime.DaysInMonth(current.Year, current.Month);
current = new DateTime(current.Year, current.Month, lastDay);
Console.WriteLine(current.ToShortDateString());
}
编辑:
这是我机器上的控制台输出:
10/5/2017
10/31/2017
11/30/2017
12/31/2017
1/31/2018
2/20/2018
2/28/2018
答案 2 :(得分:0)
public List<string> GetIntervals(DateTime startDate, DateTime endDate) {
List<string> result = new List<string>();
result.Add(startDate.ToShortDateString());
result.Add(endDate.ToShortDateString());
DateTime target = startDate;
while (target <= endDate) {
result.Add(new DateTime(target.Year, target.Month, DateTime.DaysInMonth(target.Year, target.Month)).ToShortDateString());
target = target.AddMonths(1);
}
result = result.Distinct().ToList();
result.Sort();
return result;
}
答案 3 :(得分:0)
var startDate = new DateTime(2017, 10, 5);
var endDate = new DateTime(2018, 2, 20);
var firstDayofMonth = new DateTime(startDate.Year, startDate.Month, 1).AddMonths(1);
Console.WriteLine(startDate.ToShortDateString());
while (true)
{
var dateToCompare = firstDayofMonth.AddDays(-1);
//Last month
if (dateToCompare >= endDate)
{
if (dateToCompare > endDate)
{
Console.WriteLine(endDate.ToShortDateString());
}
Console.WriteLine(dateToCompare.ToShortDateString());
break;
}
if (dateToCompare != startDate)
{
Console.WriteLine(dateToCompare.ToShortDateString());
}
firstDayofMonth = firstDayofMonth.AddMonths(1);
}