Click here to Skip to main content
15,884,176 members
Articles / .NET
Alternative
Tip/Trick

Get Quarter Starting and Ending Dates for a given Date

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
19 Mar 2011CPOL 11K   2   2
To calculate the quarter, you can use the Time Period Library for .NET:// ----------------------------------------------------------------------public static void ShowQuarterInfo( YearMonth yearBaseMonth = YearMonth.January ){ ShowQuarterInfo( DateTime.Now, yearBaseMonth );} //...
To calculate the quarter, you can use the Time Period Library for .NET:
C#
// ----------------------------------------------------------------------
public static void ShowQuarterInfo( YearMonth yearBaseMonth = YearMonth.January )
{
  ShowQuarterInfo( DateTime.Now, yearBaseMonth );
} // ShowQuarterInfo

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

  // working quarter
  Quarter quarter = new Quarter( moment );
  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


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

Cheers, Jani.

License

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


Written By
Software Developer (Senior)
Switzerland Switzerland
👨 Senior .NET Software Engineer

🚀 My Open Source Projects
- Time Period Library 👉 GitHub
- Payroll Engine 👉 GitHub

Feedback and contributions are welcome.



Comments and Discussions

 
GeneralThank you. Very useful library. Infact, I am unaware of thi... Pin
Ravi LVS18-Mar-11 16:29
Ravi LVS18-Mar-11 16:29 
GeneralReason for my vote of 5 Good Pin
Ravi LVS18-Mar-11 16:29
Ravi LVS18-Mar-11 16:29 

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.