jQuery and ASP.NET

May 21, 2009

Find the First and Last Day of the Current Quarter




A very handy piece of code by Karl that finds the first and last day of the current quarter.

C#


DateTime datetime = DateTime.Now;


int currQuarter = (datetime.Month - 1) / 3 + 1;


DateTime dtFirstDay = new DateTime(datetime.Year, 3 * currQuarter - 2, 1);


DateTime dtLastDay = new DateTime(datetime.Year, 3 * currQuarter + 1, 1).AddDays(-1);


 


Response.Write("First Day of Quarter - " + dtFirstDay.ToShortDateString() + " : " +


    "Last Day of Quarter - " + dtLastDay.ToShortDateString());




VB.NET


Dim datetime As DateTime = DateTime.Now


Dim currQuarter As Integer = (datetime.Month - 1) / 3 + 1


Dim dtFirstDay As New DateTime(datetime.Year, 3 * currQuarter - 2, 1)


Dim dtLastDay As New DateTime(datetime.Year, 3 * currQuarter + 1, 1).AddDays(-1)


 


Response.Write("First Day of Quarter - " & dtFirstDay.ToShortDateString() & " : " _


& "Last Day of Quarter - " & dtLastDay.ToShortDateString())




'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

3 Responses to "Find the First and Last Day of the Current Quarter"
  1. Rogek said...
    November 4, 2009 1:29 AM

    very useful for my application. How can i find last day of previous month using DateTime class?

  2. clanou said...
    November 5, 2009 7:15 AM

    Quarter 4 was giving me fits. I changed to...

    Dim currQuarter As Integer = CType((Date.Today.Month - 1) / 3 + 1, Integer)
    Dim dtFirstDay As Date = New DateTime(Date.Today.Year, 3 * currQuarter - 2, 1)
    Dim dtLastDay As Date = New DateTime(dtFirstDay.Year, dtFirstDay.Month + 2, 1).AddMonths(1).AddDays(-1)

  3. labilbe said...
    June 7, 2011 11:21 AM
    This comment has been removed by the author.
 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions