List the months between two dates

前端 未结 5 1460
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 14:24

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

相关标签:
5条回答
  • 2020-12-05 14:44

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

        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.

    Nov
    Dec
    Jan
    Feb
    Mar
    Apr
    May
    Jun
    Jul
    Aug
    Sep
    Oct
    
    0 讨论(0)
  • 2020-12-05 14:45

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

    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)

    //something on these lines 
    while(startdate <= enddate)
    {
    // pull out month and year
    startdate = startdate.AddMonths(1);
    }
    
    0 讨论(0)
  • 2020-12-05 14:48

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

    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));
    
    0 讨论(0)
  • 2020-12-05 15:04

    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?

    0 讨论(0)
  • 2020-12-05 15:06

    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.

    0 讨论(0)
提交回复
热议问题