Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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列出两个日期之间的月数#_C#_Date - Fatal编程技术网

C# 使用c列出两个日期之间的月数#

C# 使用c列出两个日期之间的月数#,c#,date,C#,Date,我有两个约会 Start Date End Date 02/25/2017 02/25/2018 我想列出这些日期之间的所有月份和年份。有什么提示和帮助吗 我希望最终结果如下: Month Year 2 2017 3 2017 4 2017 5 2017 6 2017 7 2017 8 2017 9 2017 10 2017 11 2017 12 2017 1 2018 2 2018

我有两个约会

Start Date    End Date
02/25/2017    02/25/2018
我想列出这些日期之间的所有月份和年份。有什么提示和帮助吗

我希望最终结果如下:

Month Year
2     2017
3     2017
4     2017
5     2017
6     2017
7     2017
8     2017
9     2017
10    2017
11    2017
12    2017
1     2018
2     2018

您可以使用以下逻辑

DateTime dt1 = new DateTime(2017, 2, 25);
DateTime dt2 = new DateTime(2018, 2, 25);

while (dt1 < dt2)
{
    Console.WriteLine(dt1.ToString("MM-yyyy"));
    dt1 = dt1.AddMonths(1);
}
DateTime dt1=新的日期时间(2017,2,25);
DateTime dt2=新的日期时间(2018,2,25);
而(dt1

请显示您拥有的代码以及您认为逻辑不正确的地方。想想您将如何手动执行此操作。。。用日历怎么办?谢谢;我还想将每个月存储到另一个变量。我已经写了代码,但它不工作。int[]months=new int[4;int i=0;while(StartDate