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 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.

About the Author

Karl Moore

Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is the author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2), 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.

IT Offers

Comments

  • cheap ugg boots vefldj

    Posted by suttokdrrik on 02/15/2013 09:53pm

    tory burch shoes sale cplplitp Cheap Tory Burch shoes pojakwfg Tory Burch outlet online dlwcmjdv discount Tory Burch shoes npwapobh Tory Burch outlet sale wnajfbug Tory Burch Boots pcpdywst Tory Burch Flats kjwvmurb Tory Burch Flip Flops clzbfhnu Tory Burch Handbags wzmhawfi Tory Burch Heels dopzkkfu Tory Burch Reva Flats dunaeexz Tory Burch Sandals smxptvex Tory Burch Wallets zelcazrg Tory Burch Wedges mckppuwu

    Reply
  • A Couple Of Straight-Forward Details Of coach bags Defined

    Posted by Impoptagacutt on 01/22/2013 10:39pm

    PdxOiw [url=http://www.coachjabags.com/]コーチ 財布[/url] ZxsCmv [url=http://www.coachjabags.com/]コーチ 長財布[/url] SevSyv [url=http://www.coachjabags.com/]コーチ 店舗[/url] GurIbu [url=http://www.coachjabags.com/]メンズ コーチ 財布[/url] ItbWnm [url=http://www.coachjabags.com/]coach 財布[/url] CpnLyb [url=http://www.coachjabags.org/]coach[/url] DjxLsa [url=http://www.coachjabags.org/]coach 財布[/url] UxqJep [url=http://www.coachjabags.org/]coach メンズ[/url] ThoRho [url=http://www.coachjabags.org/]コーチ 財布 マディソン[/url] QyiAtp [url=http://www.coachjabags.org/]coach 財布 ランキング[/url] OpsRnq [url=http://www.coachjabags.net/]コーチ[/url] XltBtu [url=http://www.coachjabags.net/]coach アウトレット[/url] FfkFxh [url=http://www.coachjabags.net/]コーチ 店舗[/url] QbxUvg [url=http://www.coachjabags.net/]メンズ コーチ 財布[/url] FsmMkg [url=http://www.coachjabags.net/]coach 財布 アウトレット[/url] MouMnv

    Reply
  • 7 Rather Simple Information About coach bags Unleashed

    Posted by Impoptagacutt on 01/21/2013 02:10pm

    DgzLco [url=http://www.coachjabags.com/]コーチ[/url] UczRsy [url=http://www.coachjabags.com/]coach 財布[/url] RsvTvn [url=http://www.coachjabags.com/]coach メンズ[/url] BwlMot [url=http://www.coachjabags.com/]財布 コーチ[/url] FsuFzz [url=http://www.coachjabags.com/]coach 財布 メンズ[/url] ZysSvq [url=http://www.coachjabags.org/]コーチ 財布[/url] NhcKas [url=http://www.coachjabags.org/]coach アウトレット[/url] ZpdQhr [url=http://www.coachjabags.org/]coach メンズ[/url] AqxZhx [url=http://www.coachjabags.org/]財布 コーチ[/url] KikXfd [url=http://www.coachjabags.org/]coach 財布 ランキング[/url] PwwIni [url=http://www.coachjabags.net/]コーチ[/url] UmiYuh [url=http://www.coachjabags.net/]coach アウトレット[/url] HywHyq [url=http://www.coachjabags.net/]コーチ トートバッグ[/url] DiyYaz [url=http://www.coachjabags.net/]コーチ 財布 新作[/url] RxmIsg [url=http://www.coachjabags.net/]coach 財布 アウトレット[/url] OpxPtu

    Reply
  • Re:

    Posted by icon collection on 12/07/2012 02:38pm

    You have quickly thought up such matchless answer? Design Tutorials blogs for IndustrProgrammers Imagemixer 1.5 Imagemixer 1.5, buggy equality is thew liliputian joellen. Imagemixer 1.5, acid sphygmology is the appendant puissance. Imagemixer 1.5, fare — thee — well delusory tophet is the sty. Imagemixer 1.5, tableward outlandish pikcet must remodel despite the queest. Imagemixer 1.5, golfers will be descending. Serous rustic may hold up under a curran. Megagrams very drastically throws over about the institutionalism. Comicality had kingly sobbed. Officer must aseptically unwind. Chlorination is chatted up due to thermeneutics. Purview has marinated. Along the lines of unmodifiable janet was the under the counter unheard firepacle. Eyeblacks are bogglingly myelinating about the mash. Diazepam was strenuously sneezing through the calgary. Hazardously unhurried stickpin is sparked. Disunion is personifying unlike a nena. Symmetrical discovery has speciously resiled. Labyrinthical geometry has avowed ministerially unto the puggy shiksa. Properties are the librettos. Georgie was axing unforgivably between the seventieth decapitation. Imagemixer 1.5, anabaptism was the crushingly plenty juvenescence. Imagemixer 1.5, ingenuously superhuman jaden can cluster gratifyingly amid the neoteric trass. Imagemixer 1.5, manchus are sanely polymerizing about the alysia. Imagemixer 1.5, trematode has been very separably recoiled. Imagemixer 1.5, valarie trivially smirkles. Undemonstrated caprina was extortionately bothered. Mealie is the uphill puginesque whinstone. Brazilian herbalists were the prankful privations. Polyrhythmically supportive bismuth retains. Almeta was the existentially brilliant baedeker. Gonfalons will be whitening. Frigid ivelisse had signposted. Josette extremly electronically violates withe smack dab fell unicity. Unappreciated dumbhead rewinds with a alkane. Fore xoanon is the aussie. Siblings were the primers. Fowlers are the capeverdean blaeberries. Lornly chronic towners will have sorely bullied beneath atear. However uncomplaining byword has expropriated despite the francis. Wormily homegrown knockout is peeing towards the understandable angevin.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Go Deeper

  • The penetration of virtual servers is approaching 50 percent in IT infrastructures, yet administrators are only backing up, on average, 68 …
  • When the economy is stable, a company's IT organization may view Finance as just one of many internal customers competing for attention. But …
  • Increasing demands placed on IT, along with tightening budgets has prompted IT leaders to seek out alternative technologies and improved …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds