Click here to Skip to main content
15,885,928 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Hi,

Can anybody pls find for me a C# code through which i can find what quarter it is for the current year .

eg: Jan,Feb,Mar Falls in Quarter1
Apr-JUne: Quarter2
July-Sept:Quarter3
Oct-Dec-Quarter4
Posted
Updated 30-Apr-22 0:56am
Comments
Ankit Rajput 4-May-11 8:05am    
We are here to help when you get any problem.
We are not here to code for you.
Have you tried something by your own?
Saumya J Pandey 4-May-11 8:23am    
Thanks Ankit for telling me that you are there to help and not to code for me. Anyways I got the logic but i am unable to do it in linq.
The logic is below
int CurrentQuarter = ((DateTime.Now.Month - 1) / 3) + 1;
and the next time before commenting to somebody's query better read the problem. The person who asks a query might be in a problem.
Ankit Rajput 4-May-11 8:32am    
I have commented according to your Question( "pls find for me a C# code"). I think now you can understand why i had written this.
Richard MacCutchan 4-May-11 9:25am    
Since you know the answer why did you post the question?
Marc A. Brown 4-May-11 12:13pm    
So, you asked for code and got upset because someone called you on it. Then you show C# code (the language you said you needed the code to be in) that will do what you're talking about. But you mention that you can't do it in linq. So first you should be more polite to potential answerers (we see a *lot* of "gimme code" questions here and they get old). Second, if you need help with linq, perhaps you need to retag your question to include that *and* mention it in the question. Third, have a great day!

Compact and easy to understand. (needs at least C# 7 because of the ValueTuple)

C#
public static class DateExtensions
{
  public static (int Year, int Quarter) GetQuarterInfo(this DateTime value)
  {
    var quarter = (value.Month + 2) / 3;

    return (value.Year, quarter);
  }
}
 
Share this answer
 
v2
If you know the answer why you post a question?
Anyway, I think this method would be nice implemented as an extension method like this

C#
static class ExtensionMethods
 {
     public static int GetQuarter(this DateTime dt)
     {
         return (dt.Month - 1) / 3 + 1;
     }
 }


and called like:

int iQuarter = DateTime.Now.GetQuarter();
 
Share this answer
 
v2
Comments
Saumya J Pandey 5-May-11 2:19am    
thanks johannesnestler... its really a help..
johannesnestler 5-May-11 16:00pm    
Glad you liked it. I think for this small Problem it's OK to ask for "give me code". So i don't Share the commenters opinion...
Saumya J Pandey 6-May-11 0:53am    
i am happy that atleast you understand the urgency in my words. :)
Below are few lines which does the trick for you. Modify it as per your need

C#
int mth = DateTime.Now.Month;
if (mth <= 3)
{
      Console.WriteLine("Quarter 1");
}
else if (mth > 3 && mth <= 6)
{
      Console.WriteLine("Quarter 2");
}
else if (mth > 6 && mth <= 9)
{
      Console.WriteLine("Quarter 3");
}
else if (mth > 9 && mth <= 12)
{
      Console.WriteLine("Quarter 4");
}



Hope this will help you out
 
Share this answer
 
Comments
Saumya J Pandey 4-May-11 8:24am    
thanks umair, but actually this is not required. the logic is
int CurrentQuarter = ((DateTime.Now.Month - 1) / 3) + 1;
The Time Period Library for .NET includes the enum YearQuarter and the class Quarter:
C#
// ----------------------------------------------------------------------
public void YearQuartersSample()
{
  Year year = new Year( 2012 );
  ITimePeriodCollection quarters = year.GetQuarters();
  Console.WriteLine( "Quarters of Year: {0}", year );
  // > Quarters of Year: 2012; 01.01.2012 - 31.12.2012 | 365.23:59
  foreach ( Quarter quarter in quarters )
  {
    Console.WriteLine( "Quarter: {0}", quarter );
  }
  // > Quarter: Q1 2012; 01.01.2012 - 31.03.2012 | 90.23:59
  // > Quarter: Q2 2012; 01.04.2012 - 30.06.2012 | 90.23:59
  // > Quarter: Q3 2012; 01.07.2012 - 30.09.2012 | 91.23:59
  // > Quarter: Q4 2012; 01.10.2012 - 31.12.2012 | 91.23:59

} // YearQuartersSample
 
Share this answer
 
The simplest solution is to check a remainder. Casting is automatic.

int thisMonth = DateTime.Now.Month;
int monthsInOneQuarter = 3;
int quarter = 0;

if (thisMonth % monthsInOneQuarter > 0)
{
  quarter = thisMonth / monthsInOneQuarter + 1;
}
else
{
  quarter = thisMonth / monthsInOneQuarter ;
}

System.Console.WriteLine("The month is from the " + quarter + ". quarter");
 
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