Results 1 to 9 of 9

Thread: Saturday and Sunday

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    70

    Saturday and Sunday

    Hi Someone can help me


    I want count between to date how many Saturday and Sunday (Or Weekend)


    Thanks!


    JP

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Just change "date1" and "date2" to your variable names (date1 must be an earlier date than date2).
    VB Code:
    1. TmpDate = date1
    2.     Do While TmpDate <= date2
    3.       Select Case Weekday(TmpDate)
    4.       Case vbSaturday, vbSunday
    5.         NumWeekEndDays = NumWeekEndDays + 1
    6.       End Select
    7.       TmpDate = DateAdd("d", 1, TmpDate)
    8.     Loop
    9.  
    10.     Msgbox "Weekend days: " & NumWeekEndDays & vbCr _
    11.                  "Weekends: " & (NumWeekEndDays/2) & vbCr _
    12.                  "Whole Weekends: " & (NumWeekEndDays\2)

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    VB Code:
    1. Option Explicit
    2.  
    3. Private Function CountSatSun(StartDate As Date, EndDate As Date) As Long
    4. Dim FirstSat As Date
    5. Dim LoopDate As Date
    6. Dim lngCount As Long
    7.  
    8.    'Do some error handling for StartDate and EndDate here
    9.    
    10.    FirstSat = DateAdd("d", -Weekday(EndDate), EndDate)
    11.  
    12.    'Initial count
    13.    lngCount = 0
    14.    LoopDate = FirstSat
    15.    Do Until LoopDate > EndDate
    16.       Select Case Weekday(LoopDate)
    17.       Case vbSaturday
    18.          If LoopDate < StartDate Then
    19.             LoopDate = DateAdd("d", 1, LoopDate)
    20.          Else
    21.             lngCount = lngCount + 1
    22.             LoopDate = DateAdd("d", 1, LoopDate)
    23.          End If
    24.       Case vbSunday
    25.          If LoopDate < StartDate Then
    26.             LoopDate = DateAdd("d", 6, LoopDate)
    27.          Else
    28.             lngCount = lngCount + 1
    29.             LoopDate = DateAdd("d", 6, LoopDate)
    30.          End If
    31.       End Select
    32.    Loop
    33.    
    34.    CountSatSun = lngCount
    35. End Function
    36.  
    37. Private Sub Command1_Click()
    38.    Text1.Text = CountSatSun(CDate(DTPicker1.Value), CDate(DTPicker2.Value))
    39. End Sub

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Oops... Didn;t know there was already an anwer.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    70
    Thanks!

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    I dislike the idea of looping to do something that can be done mathematically - if you need to do a lot of this Sat/Sun counting, looping will be expensive.

    Here's a function I just dug up that I wrote on a mainframe in VAX-11 Basic back in 1989 - it converts a DATE to a JULIAN day value. It has hooks for standard Smithsonian day 0 vs Digital VAX day zero. I got the logic from a "programmable calculator book". It really works - you would be surprised...

    Don't be alarmed by the "IF-MODIFIERS" - VAX-11 BASIC has a really great construct that allowed you to say "X=1 IF A=B"

    At any rate, using these math steps to convert your two dates to julian day #'s seems like it could yield similar results without killing the processor. Once you turn your dates into day numbers, doing some division by 7 and comparing to what you know the start date is for a day should work...

    VB Code:
    1. 1       function long ast_julian(dir%,year,month,day,offset,jul.date)
    2.         option size = real double
    3.         !       Pass    :       dir% = -1
    4.         !                       year
    5.         !                       month
    6.         !                       day
    7.         !                       julian count offset
    8.         !       Returns :       julian date of date passed minus offset
    9.         !               or
    10.         !       Pass    :       dir% = -2%
    11.         !                       julian count offset
    12.         !                       julian date of date to convert
    13.         !       Returns :       year
    14.         !                       month
    15.         !                       day
    16.         ast_julian = 0%
    17.         off.. = offset
    18.         off.. = 2400000.5       if offset = -1          ! Nov 17, 1858
    19.         off.. = 2444238.5       if offset = -2          ! Jan 01, 1980
    20.         goto figure_cal_date    if dir% = -2%
    21.         y = year
    22.         m = month
    23.         d = day
    24.         if m <= 2 then
    25.                 y = y - 1
    26.                 m = m + 12
    27.         end if
    28.         if year > 1582 or (year = 1582 and (month > 10 or (month = 10 &
    29.                                                         and day >= 15))) then
    30.                 a1 = int(y/100)
    31.                 b1 = 2 - a1 + int(a1/4)
    32.         else
    33.                 b1 = 0
    34.         end if
    35.         c1 = int(365.25 * y)
    36.         d1 = int(30.6001 * (m + 1))
    37.         jul.date = (b1 + c1 + d1 + d + 1720994.5) - off..
    38.         functionexit
    39.  figure_cal_date:
    40.         jd = jul.date + .5 + off..
    41.         i = int(jd)
    42.         f = jd - i
    43.         if i > 2299160 then
    44.                 a = int((i - 1867216.25) / 36524.25)
    45.                 b = i + 1 + a - int(a/4)
    46.         else
    47.                 b = i
    48.         end if
    49.         c = b + 1524
    50.         d = int((c - 122.1) / 365.25)
    51.         e = int(365.25 * d)
    52.         g = int((c - e) / 30.6001)
    53.         day = c - e + f - int(30.6001 * g)
    54.         month = g - 1
    55.         month = g - 13  if g > 13.5
    56.         year = d - 4716
    57.         year = d - 4715 if month < 2.5
    58.         functionend

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Why the loops? Won't the DateDiff function work for this? I didn't test thoroughly but this may be all you need.

    VB Code:
    1. ' Shows that there are 8 Sundays between the two dates.
    2. Debug.print DateDiff("ww", "01-Jan-2004", "28-Feb-2004", vbSunday)
    3.  
    4. ' Shows that there are 9 Saturdays between the two dates.
    5. Debug.print DateDiff("ww", "01-Jan-2004", "28-Feb-2004", vbSaturday)

  8. #8
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048
    heh.. there's 52 saturdays and 52 sundays

  9. #9
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    VB Code:
    1. '*****************************************************************************************
    2. '* Name:            Julian
    3. '* Author:          Mark Wilson
    4. '* Date:            23 January 2001
    5. '*
    6. '* Parameters:      lDay    --> LONG     Day number
    7. '*                  lMonth  --> LONG     Month Number
    8. '*                  lYear   --> LONG     4 digit Year Number
    9. '*
    10. '*                  Ret         DOUBLE   Julian Date
    11. '*
    12. '* Notes:           This function converts from a standard Gregorian date to the
    13. '*                  astronomical Julian Day Number (not Julian Date - which starts again
    14. '*                  at the beginning of each year at 1 fom Jan 1st)
    15. '*
    16. '*                  This function can handle dates from 4713 BC to 4713 AD+ This should be
    17. '*                  enough scope for most uses. It is also the fundamental Julian Day
    18. '*                  Number Epoch from Scaliger's Initial Epoch.
    19. '*
    20. '*                  This algorithm has been adapted from the one that appears in
    21. '*                  'Astronomy with your Personal Computer' by Peter Duffat Smith (2nd Ed)
    22. '*
    23. '* Version:         23/01/2001          Created         MW
    24. '*****************************************************************************************
    25. Public Function Julian(ByVal lDay As Long, ByVal lMonth As Long, ByVal lYear As Long) As Double
    26.  
    27.     Dim dYear As Double
    28.     Dim dMonth As Double
    29.     Dim dDay As Double
    30.    
    31.     '*********************************
    32.     '* Algorithm place holders . . .
    33.     '*********************************
    34.     Dim a As Double
    35.     Dim b As Double
    36.     Dim c As Double
    37.     Dim d As Double
    38.    
    39.     '********************************
    40.     '* Parse VB date primitive . . .
    41.     '********************************
    42.     dDay = CDbl(lDay)
    43.     dMonth = CDbl(lMonth)
    44.     dYear = CDbl(lYear)
    45.    
    46.     If dMonth < 3 Then
    47.         dMonth = dMonth + 12
    48.         dYear = dYear - 1
    49.     End If
    50.    
    51.     '**************************************************
    52.     '* Check for Julian => Gregorian change over . . .
    53.     '**************************************************
    54.    
    55.     If CLng(lYear & Format(lMonth, "00") & Format(lDay, "00")) < 15821115 Then
    56.         b = 0
    57.     Else
    58.         a = Int(dYear / 100)
    59.         b = 2 - a + Int(a / 4)
    60.     End If
    61.  
    62.     c = Int(365.25 * dYear) - 694025
    63.    
    64.     '******************************************
    65.     '* Further calculations for BC years . . .
    66.     '******************************************
    67.     If dYear < 0 Then
    68.         c = Fix((365.25 * dYear) - 0.75) - 694025
    69.     End If
    70.    
    71.     d = Int(30.6001 * (dMonth + 1))
    72.    
    73.     '******************************************
    74.     '* Add it all up to get Julian Date . . .
    75.     '******************************************
    76.     Julian = 2415020 + b + c + d + dDay - 0.5
    77.     Exit Function
    78.  
    79. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width