HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Refactor a subroutine that divides date range into quarters.

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
subroutinerangeintodividesdatethatrefactorquarters

Problem

The subroutine below, GetDetailsQuarterly, accepts two dates. Then it writes out each quarter that falls within the date range. It also gives the first and last day of the quarter. I really don't like how the beginning and ending dates of each quarter are hardcoded. I don't believe this solution accounts for leap year as well. Could someone point me in the right direction to refactor this? Thanks in advance.

GetDetailQuarterly:

Public Sub GetDetailsQuarterly(ByVal beginDate As Date, ByVal endDate As Date)

Console.WriteLine("{0}>>> date range is {1} - {2} {3}", Environment.NewLine, beginDate, endDate, Environment.NewLine)

Dim incrementingStartDate As Date = beginDate
Dim fy As New FiscalYear()

While incrementingStartDate <= endDate

    fy.qOneBegin = "1/1/" & incrementingStartDate.Year
    fy.qOneEnd = "3/31/" & incrementingStartDate.Year
    fy.qTwoBegin = "4/1/" & incrementingStartDate.Year
    fy.qTwoEnd = "6/30/" & incrementingStartDate.Year
    fy.qThreeBegin = "7/1/" & incrementingStartDate.Year
    fy.qThreeEnd = "9/30/" & incrementingStartDate.Year
    fy.qFourBegin = "10/1/" & incrementingStartDate.Year
    fy.qFourEnd = "12/31/" & incrementingStartDate.Year

    Select Case incrementingStartDate
        Case fy.qOneBegin To fy.qOneEnd
            fy.CurrentQuarter = 1
        Case fy.qTwoBegin To fy.qTwoEnd
            fy.CurrentQuarter = 2
        Case fy.qThreeBegin To fy.qThreeEnd
            fy.CurrentQuarter = 3
        Case fy.qFourBegin To fy.qFourEnd
            fy.CurrentQuarter = 4
        Case Else
            Console.WriteLine("out of range")
    End Select

    Console.WriteLine("your q{0}", fy.CurrentQuarter)
    fy.getCurrentQuarterRange()

    incrementingStartDate = incrementingStartDate.AddMonths(3)

End While
  End Sub


FiscalYear Class:

```
Public Class FiscalYear

Public Property qOneBegin() As Date

Public Property qOneEnd() As Date

Public Property qTwoBegin() As Date

Public Property qTwoEnd() As Date

Public Property

Solution

I am a bit lost in terms of what you are actually trying to accomplish, but I can give some pointers.

First, leap years do not particularly impact quarters unless you truly have some unique rules. Under normal behaviors, February 29 will be in the same quarter as February 28, and would solidly fall inside quarter 1 in your given scenario.

Secondly, a quick way to figure out the quarter of a given date (if your quarter system starts at January 1) is the following formula

Dim currentQuarter as Integer = currentDateTime.Month \ 3 + 1


Similarly, you can perform operations to arrive at quarter beginning and ending dates.

Dim quarterStart = new DateTime(currentDateTime.Year, 3 * (currentQuarter - 1) + 1, 1)
Dim quarterEnd = quarterStart.AddMonths(3).AddSeconds(-1)


For example, using today as the base, you could test the code

Dim currentDateTime = DateTime.Now

Dim currentQuarter = currentDateTime.Month \ 3 + 1
Dim quarterStart = new DateTime(currentDateTime.Year, 3 * (currentQuarter - 1) + 1, 1)
Dim quarterEnd = quarterStart.AddMonths(3).AddSeconds(-1)

Console.WriteLine(currentQuarter)
Console.WriteLine(quarterStart)
Console.WriteLine(quarterEnd)


And see these results

2
4/1/2011 12:00:00 AM
6/30/2011 11:59:59 PM

Code Snippets

Dim currentQuarter as Integer = currentDateTime.Month \ 3 + 1
Dim quarterStart = new DateTime(currentDateTime.Year, 3 * (currentQuarter - 1) + 1, 1)
Dim quarterEnd = quarterStart.AddMonths(3).AddSeconds(-1)
Dim currentDateTime = DateTime.Now

Dim currentQuarter = currentDateTime.Month \ 3 + 1
Dim quarterStart = new DateTime(currentDateTime.Year, 3 * (currentQuarter - 1) + 1, 1)
Dim quarterEnd = quarterStart.AddMonths(3).AddSeconds(-1)

Console.WriteLine(currentQuarter)
Console.WriteLine(quarterStart)
Console.WriteLine(quarterEnd)
2
4/1/2011 12:00:00 AM
6/30/2011 11:59:59 PM

Context

StackExchange Code Review Q#2240, answer score: 4

Revisions (0)

No revisions yet.