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.

Comments

  • Fans, der kan lide at fÃ¥ deres hænder og hovedtelefoner

    Posted by gnsgki360 on 07/17/2013 06:55am

    IOC og LOCOG ikke havde reageret på en anmodning om at kommentere på tidspunktet for offentliggørelsen. Den officielle sponsor, der er mest tilbøjelige til at føle sig mest forurettede ved stunt er Panasonic.England fodboldspillere brugte Dr. Dre hovedtelefoner ved Euro 2012 for at undgå pressen. Hovedtelefonerne voksede i popularitet på Beijing 2008 Olympiske Lege, efter at selskabet gav dem til basketball stjerne LeBron James, og han gav dem ud til medlemmer af Team USA.Den baghold markedsføring initiativet kommer i hælene på en protest – iværksat af amerikanske sportsfolk, herunder 400m løber Sanya Richards-Ross – kritisere regel 40 i IOC adfærdskodeks. Reglen forbyder atleter fra at nævne deres personlige sponsorer på sociale medier under legene. [url=http://nyebeatsbydrdre.bloguedobebe.com/]beats by dre[/url] Producenter frembringer deres egne beats på nettet åbner ubegrænsede samarbejdsmuligheder med hensyn til designere på et stramt budget. Den særlige udseende tilgængelig er utallige der en bred vifte af friske rekorder på markedet hver dag derude sted. I tilfælde af at du synes at købe overgår, anmeld af din web selskabet samt høre deres varer. Du er i stand til at Google eller måske Ask tilstande såsom “internet-producenter” eller “køb er bedre end online” og masser af troværdige producent internetsider vil helt sikkert producere.En anden tendens jeg opdager i trap rekorder kunne være den store brug af rumklang omkring den primære enhed. Der er typisk en enkelt pind ud værktøj put, der har en velvoksen reverb effekt sige for eksempel en grand violin, guitar, samt synth. I de fleste tilfælde en let melodi, der gentager flere gange samt modulerer op eller ned. Processen mod kaos er meget simpelt, så prøv ikke at under vurdere de grunde, der kan placeres i dette. beats [url=http://kbhretelefonerbillig.webstarts.com/]beats by dre studio[/url] Medmindre selvfølgelig, at du hellere træt, du aldrig kommer i seng, headsets i tillæg til et program, ferie kontorstol, nikkende, venter på dine vegne. Uanset om du er under metroen, workout, til hjemmet, hvor du arbejder af anden vej, for eksempel, giver næsten de fleste mennesker et headset hver eneste dag. Men når du burde “dele” enhver sang alle lytte, vil du have en persons Beats det alle.

    Reply
  • Merkt u theMonster hoofdtelefoon die zeker aanzienlijke bescherming heeft

    Posted by mrswanzi on 06/06/2013 11:39am

    [url=http://koptelefoon-monsterbeats.blogspot.com/]beats by dre[/url] De Beats goederen op te bouwen door Dr Dre heeft vele soorten hoofdtelefoons. Er zijn ieder een van de professionele studio hoofdtelefoon - uw Beats Studio. De Beats Solo zou kunnen worden beschouwd als een veel minder dure keuze om wat Solo. ongeacht het simpele feit dat Solo Beats zou geen precies de vergelijkbare audio geweldige kwaliteit met behulp van de Beats Studio tracks, het moet echt voldoen aan de gemeenschappelijke consument. [url=http://koptelefoon-monsterbeats.webspawner.com/]beats by dre[/url] Zoals bij de rest van de Beats-producten, zijn ook de urBeats verkrijgbaar in verschillende kleuren en voorzien van de nodige Beats-verwijzingen. In minder subtiele vorm is het Beats-logo aangebracht, waardoor de Beats-kenner al op afstand een set urBeats kan spotten! De urBeats zijn verkrijgbaar in een witte en zwarte uitvoering. Zo is er voor ieder wel wat wils! [url=http://koptelefoon.qsite.dk/]Monster koptelefoon[/url] Imitaties van de Beats By Dr Dre koptelefoons worden ontzettend goed gemaakt. Veel van de neppe beats by dr dre¡¯s zijn uiterlijk niet van echt te onderscheiden. Imitaties zijn soms moeilijk van echt te onderscheiden en er zijn ook een aantal Myhten en Fabels over het herkennen van imitaties. Als je zeker wilt weten dat je een origineel exemplaar aanschaft, zorg dan dat je bij een geauthoriseerde dealer koopt. Zo krijg je de garantie dat je een authentieke Beats By Dr Dre koopt.

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

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds