borderAndreaVB free resources for Visual Basic developersborder
borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2014 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (smart application)Next Topic (Syntax for excel within VB.net) New Topic New Poll Post Reply
AndreaVB Forum : VB.Net : get the number of fridays in a month
Poster Message
rtbuquia
Level: Magician

Registered: 29-05-2003
Posts: 86

icon get the number of fridays in a month

How to get the number of fridays in a given month using vb .net?

____________________________
rtbuquia

02-11-2004 at 01:37 AM
View Profile Send Email to User Show All Posts | Quote Reply
GeoffS
Level: Wizard


Registered: 29-09-2004
Posts: 643
icon Re: get the number of fridays in a month

Try This -
I've written it so that you can replace the Friday with any day by assigning the DayOfWeek value to the Constant, but Sunday has to be 7 - not Zero.


    Private Function GetNumberOfFridays(ByVal dtStart As Date, ByVal dtEnd As Date) As Integer

        Dim intDayStart As Integer, intDayEnd As Integer, intNum As Integer
        Const intFriday As Integer = 5

        'Get day of week for start date
        'and move start date forward to the next Friday
        intDayStart = dtStart.DayOfWeek
        If intDayStart = 0 Then intDayStart = 7

        If intDayStart < intFriday Then
            dtStart = (dtStart.AddDays(intFriday - intDayStart))
        ElseIf intDayStart > intFriday Then
            dtStart = (dtStart.AddDays(7 - (intDayStart - intFriday)))
        End If

        'Get day of week for end date
        'and move end date back to the previous Friday
        intDayEnd = dtEnd.DayOfWeek
        If intDayEnd = 0 Then intDayEnd = 7

        If intDayEnd < intFriday Then
            dtEnd = (dtEnd.AddDays(-7 + (intFriday - intDayEnd)))
        ElseIf intDayEnd > intFriday Then
            dtEnd = (dtEnd.AddDays(intFriday - intDayEnd))
        End If

        'get the number of days between the two dates
        intNum = DateDiff(DateInterval.Day, dtStart, dtEnd) 'dtEnd.CompareTo(dtStart)
        'divide by 7 to get number of weeks
        intNum = intNum / 7
        'the period starts AND ends with a Friday so
        'add 1 to the result to get the number of Fridays
        intNum += 1
        Return intNum


    End Function



____________________________
multi-tasking - the ability to hang more than one app. at the same time.

02-11-2004 at 10:33 AM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1652
icon Re: get the number of fridays in a month

If you ask me, answer to this question is also provided in your topic Get number of weeks. If you call the function I provided there

GetNumberOfWeeks(Now, DayOfWeek.Monday)


It will return how many mondays you have in current month. For some other month that current, pass any date in desired month and you will get number of fridays (or sundays, if you preffer).



____________________________
If you find the answer helpful, please mark this topic as solved.

02-11-2004 at 12:11 PM
View Profile Send Email to User Show All Posts | Quote Reply
GeoffS
Level: Wizard


Registered: 29-09-2004
Posts: 643
icon Re: get the number of fridays in a month

Hi Goran,
Agreed. Your previous answer covers how many of a specific weekday are in any given calendar Month, but not in a period where the start month and end month are different, which is why I though this might be a useful alternative as you can set it to ANY two dates.

____________________________
multi-tasking - the ability to hang more than one app. at the same time.

02-11-2004 at 01:14 PM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1652
icon Re: get the number of fridays in a month

No, no, I was not questioning your code, GeoffS   ,  I just said to rtbuquia that answer was already there, in one of his posts...

____________________________
If you find the answer helpful, please mark this topic as solved.

02-11-2004 at 04:08 PM
View Profile Send Email to User Show All Posts | Quote Reply
GeoffS
Level: Wizard


Registered: 29-09-2004
Posts: 643
icon Re: get the number of fridays in a month

Sorry Goran. Didn't mean to imply that I was thinking that. Just wanted to emphasise the difference between our two code options. Yours is best for a specific month, mine for an unknown period.

____________________________
multi-tasking - the ability to hang more than one app. at the same time.

02-11-2004 at 04:25 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB.Net : get the number of fridays in a month
Previous Topic (smart application)Next Topic (Syntax for excel within VB.net) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
borderAndreaVB free resources for Visual Basic developersborder
borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2014 Andrea Tincaniborder