Click here to Skip to main content
15,906,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to calculate the quarter of the year from the starting date to Next Date like first Quarter is Start from April to June so the first date of quarter and the Last Date of Quarter

Please help.
thanks in advance.
Posted
Updated 22-Oct-10 18:46pm
v2
Comments
shakil0304003 23-Oct-10 2:55am    
Your requirement is little bit confusing. Clear it what you want?

Hi

i didnt check this code, pls check this code..

int GetQuarterName(DateTime myDate)
{
    return Math.Ceiling(myDate.Month / 3);
}
DateTime GetQuarterStartingDate(DateTime myDate)
{
    return new DateTime(myDate.Year,(3*GetQuarterName(myDate))-2,1);
}
 
Share this answer
 
v2
Comments
Hiren solanki 23-Oct-10 2:20am    
added 'pre' tags for code visibility.
To calculate the quarter based on the month April, you can use the Time Period Library for .NET:
C#
// ----------------------------------------------------------------------
public static void ShowQuarterInfo()
{
  ShowQuarterInfo( DateTime.Now );
} // ShowQuarterInfo

// ----------------------------------------------------------------------
public static void ShowQuarterInfo( DateTime moment )
{
  // set start month to April
  TimeCalendar calendar = new TimeCalendar(
    new TimeCalendarConfig { YearBaseMonth = YearMonth.April } );

  // working quarter
  Quarter quarter = new Quarter( moment, calendar );
  Console.WriteLine( "Quarter start: " + quarter.FirstDayStart );
  Console.WriteLine( "Quarter end: " + quarter.LastDayStart );

  // previous quarter
  Quarter previousQuarter = quarter.GetPreviousQuarter();
  Console.WriteLine( "Previous Quarter start: " + previousQuarter.FirstDayStart );
  Console.WriteLine( "Previous Quarter end: " + previousQuarter.LastDayStart );

  // next quarter
  Quarter nextQuarter = quarter.GetNextQuarter();
  Console.WriteLine( "Next Quarter start: " + nextQuarter.FirstDayStart );
  Console.WriteLine( "Next Quarter end: " + nextQuarter.LastDayStart );
} // ShowQuarterInfo


See also this[^] question/answer.

You will find more samples in the article Time Period Library for .NET[^].

Cheers, Jani.
 
Share this answer
 
Example:
private void make(int year, int quarter)
        {
            DateTime starting, ending;
            int month = (4 + 3 * (quarter - 1)) % 12;

            starting = new DateTime(year, month, 1);
            ending = starting.AddMonths(3).AddDays(-1);
            MessageBox.Show(starting.ToString() + "," + ending.ToString());
        }

            make(2010, 1);
            make(2010, 2);
            make(2010, 3);
            make(2010, 4);
 
Share this answer
 
Comments
Dalek Dave 18-Mar-11 21:25pm    
Simple!

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