Figuring Out Quarters in .NET | CodeGuru

Figuring Out Quarters in .NET

Business applications often need to figure out quarters—the four three-month periods in any year—beginning at the start of January and going through to the end of March, then April to June, July to September, and finally, October to December. Calculating the opening and closing quarter dates for a particular date is a common task for […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 16, 2004
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Business applications often need to figure out quarters—the four three-month periods in any year—beginning at the start of January and going through to the end of March, then April to June, July to September, and finally, October to December.

Calculating the opening and closing quarter dates for a particular date is a common task for programmers. So, to save you from figuring out how to write that code, the following ready-to-run functions do it all for you:

Public Function FirstDayOfQuarter(ByVal DateIn As DateTime) _
       As DateTime
   ' Calculate first day of DateIn quarter,
   ' with quarters starting at the beginning of Jan/Apr/Jul/Oct
   Dim intQuarterNum As Integer = (Month(DateIn) - 1)  3 + 1
   Return DateSerial(Year(DateIn), 3 * intQuarterNum - 2, 1)
End Function


Public Function LastDayOfQuarter(ByVal DateIn As Date) As Date
   ' Calculate last day of DateIn quarter,
   ' with quarters ending at the end of Mar/Jun/Sep/Dec
   Dim intQuarterNum As Integer = (Month(DateIn) - 1)  3 + 1
   Return DateSerial(Year(DateIn), 3 * intQuarterNum + 1, 0)
End Function

To use either of these functions, simply pass in the date for which you wish to retrieve the quarter, and it’ll return the appropriate beginning/end date as a DateTime data type (an exact equivalent of the Date data type).

And here’s an example of how you might call these functions:

Dim CurrentQuarterStart As Date = FirstDayOfQuarter(Now)
Dim CurrentQuarterEnd As Date = LastDayOfQuarter(Now)
MessageBox.Show("Current quarter start: " & CurrentQuarterStart & _
   Chr(10) & Chr(13) & "Current quarter end: " & CurrentQuarterEnd)

About the Author

Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2, $49.99), plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.