Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine Number of Saturdays in a Given Year? 6

Status
Not open for further replies.

dobber

Programmer
Aug 18, 2000
21
0
0
US
Does anyone know of an Access procedure/function to determine the number of Saturdays in any given year (the years could include Leap Years)?

Thanks in Advance for all reponses..

 
You request was interesting. I played around and came up with the funtion below. You can cut, paste and run it as is. It will ask you for a year and respond with the number of Saturdays in that year.

Public Function NoOfSaturdays()

Dim varYear As String 'variant that will determine the year in question
Dim sDate As Variant 'the first day of the year
Dim eDate As Variant 'last day of the year
Dim intDays As Integer 'calculates the number of days till the first sat of the year
'in order to determine it's date
Dim FirstSat As Date 'first Saturday of the year used in our function for calc'ing
Dim NextSat As Date 'used to store each Saturday's date
Dim intCnt As Integer 'the variable that holds the count of the Saturdays

varYear = InputBox("What's the year?") 'get the year from the user

sDate = "01/01/" & varYear
eDate = "12/31/" & varYear

intDays = 7 - WeekDay(sDate)
FirstSat = DateAdd("d", intDays, sDate)
NextSat = FirstSat 'initializing the NextSat variable

Do While NextSat < eDate

intCnt = intCnt + 1
NextSat = DateAdd(&quot;d&quot;, 7, NextSat)
Loop

MsgBox intCnt



End Function
ljprodev@yahoo.com
Professional Development
MS Access Applications
 
Thanks Lonnie,

That worked great. Just what I needed. I had struggled a while on the problem, but could not come up with a solution that seemed simple enough (I don't like very complex code to accomplish a (seemingly) simple task).

I appreciate you help...
 
A minor variation on Lonnie's thing.

A give Day of the week must occur either 52 or 53 times in any calendar year. There are only 14 &quot;different&quot; calendars. Each day of the week will be the first day of the year. Any year starting with the same day of the week will either be a leap year or a &quot;not&quot; leap year. So &quot;7&quot; differnet days of the week * (LeapYear + NOtLeapYear) = 14 calendars.

Actually it is possible to simplify even this. There are either 365 or 366 days in the year. If there are 365 days, 52 weeks and 1 day. This, in turn, logically implies that if it is NOT a leap year, only the first &quot;day&quot; (weekday) will occur more than the mininum bunmber of times (52) and quite resonably it can only occur one more time. On the other hand, if the year is a leap year, either the first or second weekday of the year may occur more than once.

Using thse (obvious?) facts, we can easily calculate wheather the given day of the week occurs 52 or 53 times, knowing nothing except the year and the day of the week of interest. We need only to &quot;calculate&quot; the day of the week of New Years day, and (possibly) wheather it is a leap year.

Discovering wheather a given Year is a leap year is simplistic. All we need to do is derive the nubmer of days in the year, which is simplistically given by datediff for the New years day of hte given year and the following year. If it is 366, the given year is a leapyear, else, it is not.

Given some 'day of the week' it is easily compared to the day of the week of New Year's Day of the given year. If they are the same, there will ALWAYS be 53 occurances of the day of the week. Like wise, if the year IS a leap year AND the given day of the week is trhe SECOND day of the year, it will also occur 53 time in the calendar year.

IN ALL OTHER INSTANCES THE GIVEN DAY OF THE WEEK WILL OCCUR 53 TIMES IN THE CALENDAR YEAR.

Code follows.

Code:
Public Function basNumDOW_Simple(YearIn As Integer, Optional strDOW As String) As Integer

    'Calculate the number of Occurances of a Specific Day of the Week
    'in a Given Calendar Year.  Simplified Version

    'Only two Items need to be Known, and the answer is ALWAYS 52 or 53
    'Is the DOW the same as the DOW for New Years Day?
    'It it a leap year, AND the DOW is the DAY AFTER New Years.

    Dim StrtNewYr As Date
    Dim EndNewYr As Date
    Dim DOW As Integer
    Dim LeapYear As Boolean

    StrtNewYr = DateSerial(YearIn, 1, 1)        'New Years day for Input
    EndNewYr = DateSerial(YearIn + 1, 1, 1)     'New Years Day for following Year

    LeapYear = DateDiff(&quot;d&quot;, StrtNewYear, EndNewYear) Eqv 366

    If (Len(strDOW) = 0) Then
        DOW = 7
     Else
        Select Case left(strDOW, 3)
            Case Is = &quot;SUN&quot;
                DOW = 1

            Case Is = &quot;MON&quot;
                DOW = 2

            Case Is = &quot;TUE&quot;
                DOW = 3

            Case Is = &quot;WED&quot;
                DOW = 4

            Case Is = &quot;THU&quot;
                DOW = 5

            Case Is = &quot;FRI&quot;
                DOW = 6

            Case Is = &quot;SAT&quot;
                DOW = 7

            Case Else
                DOW = 7

        End Select

    End If

    If (Weekday(StrtNewYr) = DOW) Then
        basNumDOW_Simple = 53
        Exit Function
    End If

    If (Weekday(StrtNewYr) = DOW - 1 And LeapYear) Then
        basNumDOW_Simple = 53
        Exit Function
    End If

    basNumDOW_Simple = 52

End Function
MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
I am melting from the brainwaves coming through my monitor Michael! :) That deserves a star... Joe Miller
joe.miller@flotech.net
 
Thanks Joe,

All kudos gratefully accepted. Now about that JOB ....


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
Yo Mike, that is totally cool! ljprodev@yahoo.com
Professional Development
MS Access Applications
 
Lonnie,

As always, kudos are greatfully accepted. Job offers pointers to gainful employment are also welcome!


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
Michael, your code is wonderful.
Just one detail might simplify it: Instead of counting the number of days in the given year check whether 29 February exists in the given year.

 
Francescina,

O.K., but how does that 'simplify' the code?
MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
Thanks to both Michael & Ronnie, both pieces of code worked well and did exactly what I needed. However, I refuse to disclose which one I actually used in my project.

I really do appreciate all the help. I'm relatively new to Access, having been a Visual Foxpro programmer. Its going to take me a while to get comfortable with the program.

Thanks for the patience.
 
Just for fun, I don't recommend to use this code but I liked the challenge to solve the problem in a single statement.

Actually it builds on Michaels algorithm.




Public function basNumDOW(Yearin as Integer, strDOW as String) as integer

return (52 + max(0,1 - Abs( (Weekday(DateSerial(YearIn, 1, 1)) - (InStr('SUNMONTUEWEDTHUFRISAT',strDOW) - 1)/3 + 1)
* (Weekday(DateSerial(YearIn, 1, 1)) - (Instr('SUNMONTUEWEDTHUFRISAT',strDOW) - 1)/3 + 1
- (DateDiff(&quot;d&quot;, DateSerial(YearIn, 1, 1), DateSerial(YearIn + 1, 1, 1)) - 365)))
exit function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top