Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 两个日期之间的月间隔_C#_Date - Fatal编程技术网

C# 两个日期之间的月间隔

C# 两个日期之间的月间隔,c#,date,C#,Date,我有两个日期,比如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 在C#中使用DateTime对象有什么建议吗 更新#1 我首先提到的问题是现金流管理的实际情况,因此我必须更改利息、dif和civ计算的日期规范。我使用此代码的目的是: foreach (DataRow dr in DTFlussi.Rows) { D

我有两个日期,比如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
在C#中使用DateTime对象有什么建议吗

更新#1

我首先提到的问题是现金流管理的实际情况,因此我必须更改利息、dif和civ计算的日期规范。我使用此代码的目的是:

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++;
}
foreach(DTFlussi.Rows中的数据行dr)
{
DateTime d1=Convert.ToDateTime(DTFlussi.Rows[n][“data\u scadenza”]).Date;
DateTime d2=Convert.ToDateTime(DTFlussi.Rows[n+1][“data\u scadenza”]).Date;
int[]日期=重新开始日期(d1,d2);
如果(输卵管>0)
利益相关者(saldoConto,date,DTOneri,d1,d2);
else if(saldoConto=(fidoConto*-1))
{
//利息为2%
利益相关方(Saldocon、date、tassoFIDO、DTOneri、d1、d2);
}
否则如果(输卵管<0&&saldoConto<(fidoConto*-1))
{
//利息为2%e利息为4%
利益相关方(Saldocon、Fidocon、date、tassoFIDO、Tassofextrafido、DTOneri、d1、d2);
}
捕获(例外)
{
}
n++;
}
RecurPeroDate的代码为:

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)
public int[]日期(日期时间d1,日期时间d2)
{
int[]diff=新的int[2];
//2月2日
int giorniFebbraio=0;
if(DateTime.IsLeapYear(d1.Year))
giorniFebbraio=29;
其他的
giorniFebbraio=28;
尝试
{
#缅甸1区
如果(d1.Date>=新日期时间(d1.Year,1,1)和&d1.Date=新日期时间(d1.Year,1,31))
{
DateTime Mese1=新的日期时间(d1.Year,1,31)。日期;
差异[0]=(整数)(Mese1-d1)。总天数;
差异[1]=(整数)(d2-1)。总天数;
}
#端区
//注意:代码不完整,因为其他月份(2月至12月)的代码相同
这是获得余额的后续变化所必需的,在计算结束时,这就是我想要的:

创建一个数组

            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")
                                   };

我想您要查找的是每个月的最近一天。
DateTime
struct提供了一个静态的
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());
    }
public List getinterval(DateTime startDate,DateTime endDate){
列表结果=新列表();
Add(startDate.ToShortDateString());
Add(endDate.ToShortDateString());
日期时间目标=开始日期;

虽然(目标您的结果集的模式是什么?看起来有点混乱您尝试了什么?DateTime1+x DateTime1大于DateTime2之前的天数?您的意思是什么
每月间隔
以及这些值是如何生成的?您在同一时间段内有多个日期month@RvdK这将保证一个错误,特别是在二月和二月p年。正确的方法是使用
AddMonth
是的,我必须从这些日期获得月度报告,就像我写的七行一样,我还必须获得两个日期之间的天数,比如26、30、31、31、20、8,但如果我有结束月份,这是立即的
10/5/2017
10/31/2017
11/30/2017
12/31/2017
1/31/2018
2/20/2018
2/28/2018
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;
}
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);
        }