No of Sundays in a period (1 Viewer)

Ginny2222

Ginny
Local time
Today, 05:03
Joined
Oct 27, 2007
Messages
108
I want to find the number of Sundays in a period. I am asking the inputter to give me the start date of a period and the end date. Can someone suggest coding to iterate through the dates and count the weekday index 7 only. I'm getting bogged down in this.

Help appreciated.

rgs
Ginny
 

DCrake

Remembered
Local time
Today, 05:03
Joined
Jun 8, 2005
Messages
8,632
Don't know if this will help but here is a function that, in this example, populates a combo box with n number of Mondays (You could change it to Sundays).

To call this function simply enter ListMondays in your rowsource type

David


Code:
Function ListMondays(Fld As Control, id As Variant, _
    row As Variant, col As Variant, Code As Variant) _
     As Variant
    Dim intOffset As Integer
    Dim szDate As Date
    szDate = DateAdd("m", -2, Now())
    
    Select Case Code
        Case acLBInitialize            ' Initialize.
            ListMondays = True
        Case acLBOpen                    ' Open.
            ListMondays = Timer        ' Unique ID.
        Case acLBGetRowCount            ' Get rows.
            ListMondays = 9
        Case acLBGetColumnCount    ' Get columns.
            ListMondays = 1
        Case acLBGetColumnWidth    ' Get column width.
            ListMondays = -1            ' Use default width.
        Case acLBGetValue                ' Get the data.
            intOffset = Abs((9 - Weekday(szDate)) Mod 7)
            ListMondays = Format(szDate + _
             intOffset + 7 * row, "dd/mm/yyyy")
    End Select
End Function

An alternative method would be to loop through the range of dates and count the Sundays.

Code:
Dim D as Integer
Dim TestDate As Date
Dim dCnt As Integer
TestDate = Start
D = DateDiff("d",Start,End)

For n = 1 To D
   If WeekDay(TestDate) = vbSunday Then
      dCnt = dCnt +1
   End If
   TestDate = DateAdd("d",1,TestDate)
Next

Thus dCnt would return the number of Sundays between both dates
 
Last edited:

Ginny2222

Ginny
Local time
Today, 05:03
Joined
Oct 27, 2007
Messages
108
I'll give those a go and see which works. Thanks for the help.

rgs
Ginny
 

namliam

The Mailman - AWF VIP
Local time
Today, 06:03
Joined
Aug 11, 2003
Messages
11,695
Whats wrong with:
NoOfSunday = 1 + int( (DateEnd-dateStart)/7 )

Then add some code to handle the "leap" days where this is not completely right, deduct one day...
 

Ginny2222

Ginny
Local time
Today, 05:03
Joined
Oct 27, 2007
Messages
108
Hi namliam,

If you try your coding on the following 2 sets of dates you will see why this doesn't work:

from 04/01/2009 - 08/02/2009 = 6 Sundays and 05/01/2009 - 07/02/2009 = 4 Sundays. It's this extra deduction that I am having a problem with!!!

Thanks
Ginny

Whats wrong with:
NoOfSunday = 1 + int( (DateEnd-dateStart)/7 )

Then add some code to handle the "leap" days where this is not completely right, deduct one day...
 

namliam

The Mailman - AWF VIP
Local time
Today, 06:03
Joined
Aug 11, 2003
Messages
11,695
datestart = #04/jan/2009#
DateEnd = #08/feb/2009#
?1+int( (DateEnd-dateStart)/7 )
6

Seems to be fine...

datestart = #05/jan/2009#
DateEnd = #07/feb/2009#
?1+int((DateEnd-dateStart)/7 )
5

This breaks... off by the "+1" part, this is because datestart is less (in weekday) than dateend (monday vs saturday)
Add a simple IIF to check for this occurance and fix it... (i.e. Prevent the 1 from beeing added), like so:
datestart = #05/jan/2009#
DateEnd = #08/feb/2009#
?iif(weekday(datestart, vbmonday) < Weekday(dateend, vbmonday),0,1)+int( (DateEnd-dateStart)/7 )
4

while
datestart = #04/jan/2009#
DateEnd = #08/feb/2009#
?iif(weekday(datestart, vbmonday) < Weekday(dateend, vbmonday),0,1)+int( (DateEnd-dateStart)/7 )
6

Still works too :)

Good luck !
 

Ginny2222

Ginny
Local time
Today, 05:03
Joined
Oct 27, 2007
Messages
108
Thanks, mailman. One scenario that's not catered for is the date range 10/01/2009 - 07/02/2009. 5 Sundays returned when there are only 4. I tried <=, but now my original problem surfaces again. Sunday - Sunday is reporting 1 less than it should.

rgs
Ginny
 

namliam

The Mailman - AWF VIP
Local time
Today, 06:03
Joined
Aug 11, 2003
Messages
11,695
You you do it in a function it should be quite easily doable ... Adding some IF's to 'fix' the problems.
 

DCrake

Remembered
Local time
Today, 05:03
Joined
Jun 8, 2005
Messages
8,632
As per #2 To return the number of sundays within a given period you can use a simple function

Code:
Public function GetSundays(DtmLower As Date,DtmUpper As Date) As Integer

Dim D as Integer
Dim TestDate As Date
Dim dCnt As Integer
TestDate = DtmLower
D = DateDiff("d",DtmLower,DtmUpper)

For n = 1 To D
   If WeekDay(TestDate) = vbSunday Then
      dCnt = dCnt +1
   End If
   TestDate = DateAdd("d",1,TestDate)
Next

GetSundays = dCnt

End Function

To call this function on a form you would use the following syntax

Me.TxtSundays = GetSundays(Me.StartDate,Me.EndDate)

Where the fields shown in the above example match the actual field names in the form.

David
 

namliam

The Mailman - AWF VIP
Local time
Today, 06:03
Joined
Aug 11, 2003
Messages
11,695
?getsundays(#10/jan/2009# , #07/feb/2009#)
4
?getsundays(#04/jan/2009#,#08/feb/2009#)
6
?getsundays(#05/jan/2009#,#07/feb/2009#)
4


Atleast then check only the sundays... saving 6/7 checks for a sunday...
Code:
Public Function GetSundays(DtmLower As Date, DtmUpper As Date) As Integer

Dim D As Integer
Dim TestDate As Date
Dim dCnt As Integer
Dim firstSunday As Date
Dim Sundays As Integer
firstSunday = DtmLower - Weekday(DtmLower, vbMonday) + 7
Sundays = 0
Do While firstSunday + Sundays * 7 <= DtmUpper
    Sundays = Sundays + 1
Loop
GetSundays = Sundays

End Function

With less looping it should save some processing time...

But I think this will also work:
Code:
Public Function GetSundays(DtmLower As Date, DtmUpper As Date) As Integer
Dim tmpDbl As Double
Dim dtmStart As Date
Dim dtmEnd As Date
dtmStart = DtmLower - Weekday(DtmLower) + 7 ' Get the first sunday
dtmEnd = DtmUpper - Weekday(DtmUpper) ' get the last sunday
tmpDbl = 1 + Int((dtmEnd - dtmStart) / 7)
' If the start is a sunday, add one to make it work...
If Weekday(DtmLower, vbSunday) = 1 Then tmpDbl = tmpDbl + 1
GetSundays = tmpDbl

End Function
 

Ginny2222

Ginny
Local time
Today, 05:03
Joined
Oct 27, 2007
Messages
108
Thanks David, your coding works perfectly. I have it up and running. Much appreciated.

rgs
Ginny
 

Ginny2222

Ginny
Local time
Today, 05:03
Joined
Oct 27, 2007
Messages
108
Thanks Mailman for the suggestions. The IF's were getting a bit bogged down!! All working now.

rgs
Ginny
 

Ginny2222

Ginny
Local time
Today, 05:03
Joined
Oct 27, 2007
Messages
108
Hi David, there was 1 change I had to make to your coding to cater for the period starting on a Sunday.
For n = 0 To D
otherwise I was only getting 1 Sunday for the period from 11/01/2009 - 18/01/2009.

Thanks again for the help

rgs
Ginny
 

Guus2005

AWF VIP
Local time
Today, 06:03
Joined
Jun 26, 2007
Messages
2,641
Funny, i always use datediff for this purpose

Code:
?datediff("ww",#06/jan/2009#,#08/feb/2009#,vbSunday)
 5
You might want to add a day because jan 06 is on sunday.

Enjoy!
 

DCrake

Remembered
Local time
Today, 05:03
Joined
Jun 8, 2005
Messages
8,632
If you look in the sample databases board you will find an entry I made that contains a table that is is full of dates which can be used to get any type of information about a date from it. It can used in conjuction with any other application.

David
 

Users who are viewing this thread

Top Bottom