Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get the end date for the quarter as below?

Q1 - 30th April
Q2 - 31st July
Q3 - 30th Oct
Q4 - 31st Jan(of next year)

What I have tried:

dueDate = new DateTime(year, MONTHS_IN_A_QUARTER * currQuarter + 1, 1).AddDays(-1);
Posted
Updated 5-May-20 23:29pm
Comments
Richard MacCutchan 5-May-20 9:41am    
And? What is the question?
CHill60 5-May-20 9:46am    
Those are strange quarters!
Q1 = 1st Jan to 31st March
Q2 = 1st April to 30th June
Q3 = 1st July to 30th September
Q4 = 1st October to 31st December
gggustafson 5-May-20 10:03am    
Why do you think that?
CHill60 5-May-20 10:07am    
OP has Q4 finishing on 31st Jan of the following year and has 4 months in Q1
gggustafson 5-May-20 10:14am    
OP probably fixed it before I read it.

You must split the calculation into smaller pieces
instead of trying to stuff it into one single line.
This will make it clearer to understand.

The first step is to calculate the first day of a quarter like this:
C#
var firstDayOfQuarter = new DateTime(year, 1 + (quarter - 1) * 3, 1);

Then you have to add 4 months and to subtract 1 day.
C#
var dueDate = firstDayOfQuarter.AddMonths(4).AddDays(-1);

A method for calculation would look like this:
C#
DateTime GetDueDate(int year, int quarter)
{
    var firstDayOfQuarter = new DateTime(year, 1 + (quarter - 1) * 3, 1);
    var dueDate = firstDayOfQuarter.AddMonths(4).AddDays(-1);

    return dueDate;
}
 
Share this answer
 
Comments
Maciej Los 6-May-20 4:22am    
5ed!
TheRealSteveJudge 6-May-20 4:38am    
Thank you Maciej!
If you would like to get next month end date, try this:
C#
DateTime today = DateTime.Today;
DateTime EndOfNextMonth = today.AddMonths(2).AddDays(-today.Day);//add 2 months and subtract days passed in this month

It returns 2020-06-30 for today's date: 2020-05-06

For end of quarter:
C#
DateTime EndOfQuarter = today.AddMonths(3-((today.Month+1) % 3)).AddDays(-today.Day);

It returns 2020-07-31 for today's date: 2020-05-06

You can test it on this example:
C#
DateTime today = DateTime.Today;

for(int i = 1; i<=12; i++)
{
	DateTime tmp = new DateTime(2020, i, 1);
	int quarter = ((tmp.AddMonths(-1).Month +2) / 3);
	//int quarter = ((tmp.Month +1) / 3); - displays 0 in january
	int m2a = 3-((tmp.Month+1) % 3) ;
	DateTime eoq = tmp.AddMonths(m2a).AddDays(-tmp.Day);
	Console.WriteLine($"Date: {tmp.ToString("yyyy-MM-dd")} => EndOfQuarter {quarter}: {eoq.ToString("yyyy-MM-dd")}");
}


Result:
Date: 2020-01-01 => EndOfQuarter 4: 2020-01-31
Date: 2020-02-01 => EndOfQuarter 1: 2020-04-30
Date: 2020-03-01 => EndOfQuarter 1: 2020-04-30
Date: 2020-04-01 => EndOfQuarter 1: 2020-04-30
Date: 2020-05-01 => EndOfQuarter 2: 2020-07-31
Date: 2020-06-01 => EndOfQuarter 2: 2020-07-31
Date: 2020-07-01 => EndOfQuarter 2: 2020-07-31
Date: 2020-08-01 => EndOfQuarter 3: 2020-10-31
Date: 2020-09-01 => EndOfQuarter 3: 2020-10-31
Date: 2020-10-01 => EndOfQuarter 3: 2020-10-31
Date: 2020-11-01 => EndOfQuarter 4: 2021-01-31
Date: 2020-12-01 => EndOfQuarter 4: 2021-01-31
 
Share this answer
 
v5
Comments
phil.o 6-May-20 7:31am    
5'd
Maciej Los 6-May-20 7:36am    
Thanks!
TheRealSteveJudge 6-May-20 8:42am    
Good examples! 5*
Maciej Los 6-May-20 9:05am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900