Click here to Skip to main content
15,887,683 members
Articles / .NET
Tip/Trick

Get Quarter Starting and Ending Dates for a given Date

Rate me:
Please Sign up or sign in to vote.
3.57/5 (4 votes)
8 Aug 2010CPOL 55.6K   4   14
Get Quarter Starting and Ending Dates for a given Date
When you want the Quarter Starting and Ending dates for a given date, here are simple functions to calculate.

//whichQtr =  0 - present quarter
//           -1 - previous quarter
//            1 - next quarter
public static DateTime QuarterEnddate(DateTime curDate, int whichQtr)
{
   int tQtr = (curDate.Month - 1) / 3 + 1 + whichQtr;
   return new DateTime(curDate.Year, (tQtr * 3) + 1, 1).AddDays(-1);
}

public static DateTime QuarterStartDate(DateTime curDate, int whichQtr)
{
   return QuarterEnddate(curDate, whichQtr).AddDays(1).AddMonths(-3);
}

License

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionqtr 4 doesn't work Pin
zell7111-Jul-17 15:40
zell7111-Jul-17 15:40 
BugBug Pin
Mike_Liu18-Mar-15 2:54
Mike_Liu18-Mar-15 2:54 
GeneralAnyway, I will rewrite function to accept starting month of ... Pin
Ravi LVS11-Aug-10 18:36
Ravi LVS11-Aug-10 18:36 
GeneralI removed it intentionally, because it is creating some ambi... Pin
Ravi LVS11-Aug-10 18:31
Ravi LVS11-Aug-10 18:31 
GeneralHello Ravi, where is your QuarterOfDate() method? I think, i... Pin
Kunal Chowdhury «IN»9-Aug-10 22:55
professionalKunal Chowdhury «IN»9-Aug-10 22:55 
General- Kunal No No. Most of the times, every one needs quarter o... Pin
Ravi LVS21-Jul-10 7:59
Ravi LVS21-Jul-10 7:59 
GeneralRavi, I think Luc here is right. The QuarterOfDate() should ... Pin
Kunal Chowdhury «IN»21-Jul-10 6:53
professionalKunal Chowdhury «IN»21-Jul-10 6:53 
Ravi, I think Luc here is right. The QuarterOfDate() should return 1,2,3,4 and not 4,1,2,3. Reason behind this is: we need to check the DateTime for English year only [MAX time and MAX of your readers should need it] and for that Jan, Feb, Mar should return 1 whereas Apr should return you 2. Please improve your Tip/Trick with proper code.
All the best.

And yes, there is nothing called "Negative Voting". We always want to share our knowledge here and also want to help others by suggesting them in various ways and Luc did the same thing. Please don't take it in negative way and keep sharing your knowledge here. We want to see more of your tips/articles here. Keep up the good work. Thumbs Up | :thumbsup:
General- Luc Pattyn In India 1st qtr = Apr, May, Jun, 2nd Qtr = J... Pin
Ravi LVS20-Jul-10 8:50
Ravi LVS20-Jul-10 8:50 
GeneralReason for my vote of 2 bug Pin
Luc Pattyn20-Jul-10 7:48
sitebuilderLuc Pattyn20-Jul-10 7:48 
GeneralQuarterOfDate seems quite wrong, it numbers the quarters as ... Pin
Luc Pattyn20-Jul-10 7:47
sitebuilderLuc Pattyn20-Jul-10 7:47 
GeneralReason for my vote of 5 Very Helpful. Pin
Kunal Chowdhury «IN»19-Jul-10 2:35
professionalKunal Chowdhury «IN»19-Jul-10 2:35 
GeneralAwesome. Nice to see the code. It will be very useful for th... Pin
Kunal Chowdhury «IN»19-Jul-10 2:28
professionalKunal Chowdhury «IN»19-Jul-10 2:28 
GeneralThank you, I added the function as requested by you. Pin
Ravi LVS18-Jul-10 18:27
Ravi LVS18-Jul-10 18:27 
GeneralSuper... :) But you can include one method which will return... Pin
Kunal Chowdhury «IN»17-Jul-10 4:04
professionalKunal Chowdhury «IN»17-Jul-10 4:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.