Pec1983
Pec1983

Reputation: 376

VB.NET - Given a date, how can I get the date of last four fridays?

Given today's date want to get the date of the each Friday for the last four weeks.

Upvotes: 0

Views: 678

Answers (5)

dbasnett
dbasnett

Reputation: 11773

Try this. It doesn't use a loop to find the starting Friday.

        Dim someDate As DateTime = DateTime.Now

        If someDate.DayOfWeek <> DayOfWeek.Friday Then
            'do the math to get a Friday
            someDate = someDate.AddDays(DayOfWeek.Friday - someDate.AddDays(1).DayOfWeek - 6)
        End If

        Dim last4Fridays As New List(Of DateTime) From {someDate, someDate.AddDays(-7), someDate.AddDays(-14), someDate.AddDays(-21)}

All of the other suggestions have used a loop to find the starting Friday. If this code is used infrequently then how the starting Friday is determined might not matter.

edit: as function

Function FindLastFourFridays(someDate As DateTime) As List(Of DateTime)
    'Find first Friday to include
    If someDate.DayOfWeek <> DayOfWeek.Friday Then
        someDate = someDate.AddDays(DayOfWeek.Friday - someDate.AddDays(1).DayOfWeek - 6)

        ' uncomment these two lines if you do not want initial someDate.DayOfWeek = DayOfWeek.Friday to be included
        'Else 
        '    someDate = someDate.AddDays(-7)
    End If
    'build the return (four fridays)
    Dim last4Fridays As New List(Of DateTime) From {someDate, someDate.AddDays(-7), someDate.AddDays(-14), someDate.AddDays(-21)}
    Return last4Fridays
End Function

Upvotes: 1

genespos
genespos

Reputation: 3311

Here is my way:

Function Last4Friday(ByVal StartDate As Date) As array
    Dim L4F()
    Dim mDate as date = StartDate
    For value As Integer = 1 To 7
        mDate = mDate.AddDays(-1)
        If mDate.DayOfWeek = DayOfWeek.Friday Then
        L4F = {mDate, mDate.AddDays(-7), mDate.AddDays(-14), mDate.AddDays(-21)}
        exit for
    End If
    Next
Return L4F

End Function

Edit: If you need to check the inserted date and you want it returned in the array you may simply use:

Dim mDate as date = StartDate.AddDays(1)

instead of

Dim mDate as date = StartDate

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460350

Here is an easy LINQ approach:

Dim today = Date.Today
Dim lastFridays = From d In Enumerable.Range(0, Int32.MaxValue)
                  Let dt = today.AddDays(-d)
                  Where dt.DayOfWeek = DayOfWeek.Friday
                  Select dt
Dim lastFourFridays As Date() = lastFridays.Take(4).ToArray()

Since it's not the most efficient approach, here is a query that is still readable and maintainable but only searches the first friday and then takes only every 7th day:

Dim lastFriday = lastFridays.First()  ' reuse of above query '
Dim fridays = From d In Enumerable.Range(0, Int32.MaxValue)
              Let dt = lastFriday.AddDays(-d * 7)
              Select dt
Dim lastFourFridays As Date() = fridays.Take(4).ToArray()

Upvotes: 3

NeverHopeless
NeverHopeless

Reputation: 11233

You may consume this one, which returns a list of such dates and excludes the one if the specifiedDate date is Friday:

Public Shared Function GetLastFourFridays(specifiedDate As DateTime) As List(Of DateTime)
    Dim dtm As New List(Of DateTime)()
    Dim dt As DateTime = specifiedDate
    For i As Integer = 0 To 6
        dt = dt.AddDays(-1)
        If dt.DayOfWeek = DayOfWeek.Friday Then
            dtm.Add(dt)
            Exit For
        End If
    Next
    dtm.Add(dt.AddDays(-7))
    dtm.Add(dt.AddDays(-14))
    dtm.Add(dt.AddDays(-21))

    Return dtm
End Function

and the way you use it is:

Dim dtm As List(Of DateTime) = GetLastFourFridays(DateTime.Now)

For Each d As var In dtm
    Console.WriteLine(String.Format("Date: {0}, Day: {1}", d.ToString(), [Enum].Parse(GetType(DayOfWeek), d.DayOfWeek.ToString())))
Next

Upvotes: 1

Pec1983
Pec1983

Reputation: 376

This function does not need to be passed a date it picks up today's date and gets the last four Friday's from today. It can be changed around to get any day of the week.

    Dim todaysDate As Date = Date.Today
    Dim oldDay As Integer
    Dim thisWeek As Date

    Dim firstWeek As Date
    Dim secondWeek As Date
    Dim thirdWeek As Date
    Dim fourthWeek As Date


    'finds the Friday of the end of the current week No mattter what day you are working

    Dim daycount As Integer
    'use this to check specific dates "Dim datetime As New DateTime(2015, 4, 13)"
    oldDay = Weekday(todaysDate)
    thisWeek = todaysDate

    If oldDay < 6 Then
        daycount = 6 - oldDay
        thisWeek = thisWeek.AddDays(+daycount)
    ElseIf oldDay > 6 Then
        daycount = oldDay - 6
        thisWeek = thisWeek.AddDays(-daycount)
    End If

    Dim currentDate As Date = Now
    Do While Not currentDate.DayOfWeek = DayOfWeek.Friday
        currentDate = currentDate.AddDays(-1)
    Loop

    fourthWeek = currentDate.AddDays(-21)
    thirdWeek = currentDate.AddDays(-14)
    secondWeek = currentDate.AddDays(-7)
    firstWeek = currentDate

Upvotes: 0

Related Questions