Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI,

How do i get the start and end dates of last quarter using C#?

Regards,
Ramakrishna
Posted

I looks a trivial task to me: suppose the quarters are defined this way:

  1. January 1 - March 31
  2. April 1 - June 30
  3. July 1 - September 30
  4. October 1 - December 31

Then you have to find the quarter containing the current date (It's number four in our case) and finally subtract one (set number 4 if the result is 0).

Implementing such an algorithm in C# should be simple. What are your doubts about?
:)
 
Share this answer
 
Comments
ramakrishna Namburu 4-Nov-10 0:27am    
I do not want to specify any particular dates for the quarter start and end days like above, because my quarters here are different from the quarters at client place, so i want to get only pervious quarter start and end date, even i am not worried what quarter is that.
Code below gives us the start date and end dates of previous quarter of the current day. :)
private void button1_Click(object sender, EventArgs e)
        {
            DateTime startDate = default(DateTime);
            DateTime endDate = default(DateTime);
            string lastQuarter = GetLastQuarterDates(DateTime.Now.Month);
            SetLastQuarterDates(out startDate, out endDate, lastQuarter);
            label1.Text = Convert.ToString(startDate);
            label2.Text = Convert.ToString(endDate);
        }  

private void SetLastQuarterDates(out DateTime startDate, out DateTime endDate, string quarter)
        {
            int startMonth = 0;
            int startYear = DateTime.Now.Year;
            int startDay = 1;
            int endMonth = 0;
            int endYear = DateTime.Now.Year;
            int endDay = 31;

            switch (quarter)
            {
                case "Q1":
                    startMonth = 1;
                    endMonth = 3;
                    endDay = 30;
                    break;

                case "Q2":
                    startMonth = 4;
                    endMonth = 6;
                    break;

                case "Q3":
                    startMonth = 7;
                    endMonth = 9;
                    endDay = 30;
                    break;

                case "Q4":
                    startMonth = 10;
                    startYear = DateTime.Now.Year - 1;
                    endMonth = 12;
                    endYear = DateTime.Now.Year - 1;
                    break;
            }

            startDate = new DateTime(startYear, startMonth, startDay);
            endDate = new DateTime(endYear, endMonth, endDay);
        }

        private string GetLastQuarterDates(int month)
        {
            string quarter = string.Empty;

            if (month >= 1 && month <= 3)
            {
                quarter = "Q4";
            }
            if (month >= 4 && month <= 6)
            {
                quarter = "Q1";
            }
            if (month >= 7 && month <= 9)
            {
                quarter = "Q2";
            }
            if (month >= 10 && month <= 12)
            {
                quarter = "Q3";
            }

            return quarter;
        }
 
Share this answer
 
v2
Comments
Dalek Dave 17-Mar-11 4:58am    
Edited for Code Block
Jrvansant 9-Aug-18 13:59pm    
June has 30 days, not 31. March has 31 days, not 30.
Check out the Time Period Library for .NET[^]:
C#
// ----------------------------------------------------------------------
public static void DaysOfPastQuarter( DateTime moment )
{
  // example of fiscal year, which starts at October
  TimeCalendar calendar = new TimeCalendar( new TimeCalendarConfig { YearBaseMonth = YearMonth.October } );
  Quarter currentQuarter = new Quarter( moment, calendar );
  Quarter pastQuarter = currentQuarter.GetPreviousQuarter();

  Console.WriteLine( "Start: " + pastQuarter.FirstDayLast );
  Console.WriteLine( "End: " + pastQuarter.LastDayStart );
} // DaysOfPastQuarter


Cheers, Jani.
 
Share this answer
 
v2
Comments
Dalek Dave 17-Mar-11 4:58am    
Good answer.
See here[^].
 
Share this answer
 

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