how i can get dates ranges list between 2 dates
for example,
start date
is 12 Februaryend date
is 23 May
so, the result must be like
12-Feb to 28-Feb
1-Mar to 31-Mar
1-April to 30-April
1-May to 23-May
how i can get dates ranges list between 2 dates
for example,
start date
is 12 Februaryend date
is 23 May
so, the result must be like
12-Feb to 28-Feb
1-Mar to 31-Mar
1-April to 30-April
1-May to 23-May
Let's suppose you are checking only one year, i.e. the current one (2018).
Of course you can make StartDate
and EndDate
parametric.
There's nothing special in the code below; I used "d-MMMM"
as date format since you seemed to ask for the minimum number of days ("d"
does that, i.e. selects 1
and not 01
for the first day of a month) and the "long" name of the month ("MMMM" does that, check this answer).
If instead you just want the abbreviation, i.e. "Feb" for "February", you can use "MMM" (check this other answer).
DateTime StartDate = new DateTime(2018, 2, 12);
DateTime EndDate = new DateTime(2018, 5, 23);
for (int i = 0; i <= EndDate.Month - StartDate.Month; i++)
{
DateTime FirstDay = new DateTime(2018, StartDate.Month + i, 1);
if (i == 0)
{
FirstDay = StartDate;
}
DateTime LastDay = new DateTime(2018, StartDate.Month + i, DateTime.DaysInMonth(2018, FirstDay.Month));
if (i == EndDate.Month - StartDate.Month)
{
LastDay = EndDate;
}
Console.WriteLine(FirstDay.ToString("d-MMMM") + " to " + LastDay.ToString("d-MMMM"));
}
Console.ReadLine();
You can also use the following code to make it "cross-year":
static void Main(string[] args)
{
PrintDateList(new DateTime(2018, 2, 12), new DateTime(2018, 5, 23));
PrintDateList(new DateTime(2018, 11, 12), new DateTime(2019, 2, 23));
Console.ReadLine();
}
private static void PrintDateList(DateTime StartDate, DateTime EndDate)
{
Console.WriteLine("");
int TotalMonths = EndDate.Month - StartDate.Month +
(EndDate.Year - StartDate.Year) * 12;
int CurrentYear = StartDate.Year;
int MonthsToSubtract = 0;
for (int i = 0; i <= TotalMonths; i++)
{
int CurrentMonth = StartDate.Month + i;
if (StartDate.Month + i > 12)
{
if ((StartDate.Month + i) % 12 == 1)
{
CurrentYear++;
Console.WriteLine(CurrentYear.ToString());
MonthsToSubtract = StartDate.Month + i - 1;
}
CurrentMonth = StartDate.Month + i - MonthsToSubtract;
}
DateTime FirstDay = new DateTime(CurrentYear, CurrentMonth, 1);
if (i == 0)
{
FirstDay = StartDate;
}
DateTime LastDay = new DateTime(CurrentYear, CurrentMonth, DateTime.DaysInMonth(CurrentYear, FirstDay.Month));
if (i == TotalMonths)
{
LastDay = EndDate;
}
Console.WriteLine(FirstDay.ToString("d-MMMM") + " to " + LastDay.ToString("d-MMMM"));
}
}
The output is
12-February to 28-February
1-March to 31-March
1-April to 30-April
1-May to 23-May12-November to 30-November
1-December to 31-December
1-January to 31-January
1-February to 23-February
Since you asked, I replaced Console.WriteLine
with a List
:
private static void PrintDateList(DateTime StartDate, DateTime EndDate)
{
List<Tuple<DateTime>> EventsDatesRangeList = new List<Tuple<DateTime>>();
Console.WriteLine("");
int TotalMonths = EndDate.Month - StartDate.Month +
(EndDate.Year - StartDate.Year) * 12;
int CurrentYear = StartDate.Year;
int MonthsToSubtract = 0;
for (int i = 0; i <= TotalMonths; i++)
{
int CurrentMonth = StartDate.Month + i;
if (StartDate.Month + i > 12)
{
if ((StartDate.Month + i) % 12 == 1)
{
CurrentYear++;
Console.WriteLine(CurrentYear.ToString());
MonthsToSubtract = StartDate.Month + i - 1;
}
CurrentMonth = StartDate.Month + i - MonthsToSubtract;
}
DateTime FirstDay = new DateTime(CurrentYear, CurrentMonth, 1);
if (i == 0)
{
FirstDay = StartDate;
}
DateTime LastDay = new DateTime(CurrentYear, CurrentMonth, DateTime.DaysInMonth(CurrentYear, FirstDay.Month));
if (i == TotalMonths)
{
LastDay = EndDate;
}
Console.WriteLine(FirstDay.ToString("d-MMMM") + " to " + LastDay.ToString("d-MMMM"));
EventsDatesRangeList.Add(new Tuple<DateTime>(FirstDay));
EventsDatesRangeList.Add(new Tuple<DateTime>(LastDay));
}
}