Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all months names between two given dates [duplicate]

Tags:

c#

I am trying to make a function which gives all month name between two dates in c#

List<string> liMonths = MyFunction(date1,date2);

my function is

MyFunction(DateTime date1,DateTime date2)
{

//some code
return listOfMonths;
}

can you help me how could i do this

like image 555
Amit Bisht Avatar asked Sep 06 '13 08:09

Amit Bisht


People also ask

How do I get months between two dates in SQL?

MONTHS_BETWEEN returns number of months between dates date1 and date2 . If date1 is later than date2 , then the result is positive. If date1 is earlier than date2 , then the result is negative. If date1 and date2 are either the same days of the month or both last days of months, then the result is always an integer.

How do I print the months between two dates in Python?

date(2020, 1, 1) today = datetime. date. today() for month in months_between(start_of_2020, today): print(month. strftime("%B %Y")) # January 2020, February 2020, March 2020, …


1 Answers

No linq-solution yet?

var start = new DateTime(2013, 1, 1);
var end = new DateTime(2013, 6, 22);

// set end-date to end of month
end = new DateTime(end.Year, end.Month, DateTime.DaysInMonth(end.Year, end.Month));

var diff = Enumerable.Range(0, Int32.MaxValue)
                     .Select(e => start.AddMonths(e))
                     .TakeWhile(e => e <= end)
                     .Select(e => e.ToString("MMMM"));
like image 150
sloth Avatar answered Sep 20 '22 04:09

sloth