C# 列出两个日期之间的月份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11930565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:45:19  来源:igfitidea点击:

List the months between two dates

c#

提问by user1594760

I would like to know when given start and end dates, how can the months and year(s) between the two dates be retrieved.

我想知道何时给出开始和结束日期,如何检索两个日期之间的月份和年份。

Eg: Start Date: '1/1/2011'(mm/dd/yyyy)and End date :'11/30/2011'. The months and year to be retrieved are January,2011; February,2011; March,2011; and so on till November,2011

例如:Start Date: '1/1/2011'(mm/dd/yyyy)End date :'11/30/2011'。要检索的月份和年份是January,2011; February,2011; March,2011; and so on till November,2011

回答by bhuvin

Use this formula :

使用这个公式:

((date1.Year - date2.Year) * 12) + date1.Month - date2.Month

And u get the number of months .

你得到月数。

Use this in an appropriate way and you will crack it.

以适当的方式使用它,您将破解它。

Welcome to Stackoverflow , Here Hardwork is your part and giving thinking lines for that is for the fraternity to do when you have a problem.

欢迎来到 Stackoverflow ,这里是您的职责所在,并为此提供思路供兄弟会在您遇到问题时执行。

回答by Jodrell

Here we go

开始了

    public static IEnumerable<Tuple<string, int>> MonthsBetween(
            DateTime startDate,
            DateTime endDate)
    {
        DateTime iterator;
        DateTime limit;

        if (endDate > startDate)
        {
            iterator = new DateTime(startDate.Year, startDate.Month, 1);
            limit = endDate;
        }
        else
        {
            iterator = new DateTime(endDate.Year, endDate.Month, 1);
            limit = startDate;
        }

        var dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
        while (iterator <= limit)
        {
            yield return Tuple.Create(
                dateTimeFormat.GetMonthName(iterator.Month),
                iterator.Year);                
            iterator = iterator.AddMonths(1);
        }
    }

And obviously you call it like this

显然你这样称呼它

var startDate = DateTime.ParseExact("01/01/2011", "MM/dd/yyyy");
var endDate = DateTime.ParseExact("11/30/2011", "MM/dd/yyyy");

var months = MonthsBetween(startDate, endDate);

The results should be something like

结果应该是这样的

{
    {  "January", 2011  },
    {  "February", 2011  },
    {  "March", 2011  },
    {  "April", 2011  },
    {  "May", 2011  },
    {  "June", 2011  },
    {  "July", 2011  },
    {  "August", 2011  },
    {  "September", 2011  },
    {  "October", 2011  },
    {  "November", 2011  },
}

The month names being dependent on your culture which, I think, is exactly what you asked for, right?

月份名称取决于您的文化,我认为这正是您所要求的,对吗?

回答by V4Vendetta

You can start off with a while loop where the start date is less than or equal to the end date

您可以从开始日期小于或等于结束日期的 while 循环开始

Within it pull out the relevant Month and year part from the start date and store them (maybe you have a concrete class or a as a ), then increment the start date by 1 month using AddMonths(1)

在其中从开始日期中提取相关的月份和年份部分并存储它们(也许您有一个具体的类或 a 作为 a ),然后使用将开始日期增加 1 个月 AddMonths(1)

//something on these lines 
while(startdate <= enddate)
{
// pull out month and year
startdate = startdate.AddMonths(1);
}

回答by Pankajya

You can use Enumerable.Range function to get the month and year between two dates,

您可以使用 Enumerable.Range 函数来获取两个日期之间的月份和年份,

var start = new DateTime(2011, 1, 1);
var end = new DateTime(2011, 11, 30);

var diff = Enumerable.Range(0, 13).Select(a => start.AddMonths(a))
           .TakeWhile(a => a <= end)
           .Select(a => String.Concat(a.ToString("MMMM") + ", " + a.Year));

回答by Simba

You can utilize Linq by employing Enumerable.Range(startIndex, endIndex).
Example below:

您可以通过使用 Enumerable.Range(startIndex, endIndex) 来使用 Linq。
下面的例子:

    private string[] GetMonthsBetweenDates(DateTime deltaDate, int TotalMonths)
{
    var monthsBetweenDates = Enumerable.Range(0, TotalMonths)
                                       .Select(i => deltaDate.AddMonths(i))
                                       .OrderBy(e => e)
                                       .AsEnumerable();

    return monthsBetweenDates.Select(e => e.ToString("MMM")).ToArray();
}

The code above should produce the following results, assuming the current month is November.

假设当前月份是 11 月,上面的代码应该会产生以下结果。

Nov
Dec
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct