Solved

DateDiff without Sunday

Posted on 1998-06-29
7
530 Views
Last Modified: 2008-03-17
How can I compute the number of days between two Dates so that there is no Sunday in the result.
0
Comment
Question by:kea
7 Comments
 

Author Comment

by:kea
ID: 1975942
-
0
 
LVL 18

Expert Comment

by:deighton
ID: 1975943
Here is a code fragment that i hope will work for you

    Dim X, y
    Dim Z As Long

    X = CVDate("01-01-1998")   'YOUR DATES ARE HERE   (First Lowest Date)
    y = CVDate("09-01-1998")   ' Higher (later Date)

    Do Until X = y

        X = X + 1
        If Weekday(X, 1) <> 1 Then
            Z = Z + 1
        End If

    Loop

    MsgBox "NO OF DAYS EX SUNDAY " + Str(Z)
0
 
LVL 4

Accepted Solution

by:
tomook earned 50 total points
ID: 1975944
' I left some debuggers in but commented out in case
' you want to pick the routine apart.
Function DateDiffWithoutSundays(LowDate As Date, HighDate As Date)
    ' Figure out the number of days between two dates
    ' not counting Sunday.
   
    Dim DayOfLowDate As Long, DayOfHighDate As Long
    Dim DaysBetween As Long
    Dim DayOfWeekLow As Integer, DayOfWeekHigh As Integer
    Dim SundaysPast As Long
    'Dim sTmp1 As String
   
    ' Figure out the raw number of days between.
    ' Note that these are assumed to be "even" days,
    ' i.e. no time of day included.
    DaysBetween = CLng(HighDate - LowDate)
   
    'sTmp1 = "Date 1: " & Format(LowDate, "mm/dd/yyyy") & vbCrLf
    'sTmp1 = sTmp1 & "Date 2: " & Format(HighDate, "mm/dd/yyyy") & vbCrLf
    'sTmp1 = sTmp1 & "Days Difference = " & DaysBetween
    'Debug.Print sTmp1
   
    ' DatePart("w", ..., vbSunday) returns the day of the week
    ' with Sunday = 1 and Saturday = 7
    DayOfWeekLow = DatePart("w", LowDate, vbSunday)
    DayOfWeekHigh = DatePart("w", HighDate, vbSunday)
   
    'sTmp1 = Format(LowDate, "mm/dd/yyyy") & " is day " & DayOfWeekLow & vbCrLf
    'sTmp1 = sTmp1 & Format(HighDate, "mm/dd/yyyy") & " is day " & DayOfWeekHigh
    'Debug.Print sTmp1
   
    ' Figure out how many whole weeks passed by using
    ' an integer divide. We could also use DateDiff,
    ' but we have no compelling reason in this case.
    SundaysPast = DaysBetween \ 7
   
    ' If a partial week passed, and it crossed Sunday, add another week
    ' to WeeksPast. We know a partial week passed because:
    ' 1. DayOfWeekLow <> DayOfWeekHigh AND
    ' 2. DayOfWeekLow > DayOfWeekHigh
    ' Looking at the logic, we need only check for the second case.
    If DayOfWeekLow > DayOfWeekHigh Then SundaysPast = SundaysPast + 1
   
    'sTmp1 = SundaysPast & " sundays went by"
    'Debug.Print sTmp1
   
    ' Now it is easy.
    DaysBetween = DaysBetween - SundaysPast
       
    'sTmp1 = DaysBetween & " days between, not counting Sundays"
    'Debug.Print sTmp1

    DateDiffWithoutSundays = DaysBetween
End Function

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 17

Expert Comment

by:ramrom
ID: 1975945
Why did you reject deighton's answer? We who invest time in answering questions are helped by some feedback. My first hit was "efficiency", yet on my 686x166 w/65 MB RAM it processed 180,000 days in 3 seconds. tomook's answer, of course is optimal, since it avoids the loop.
0
 
LVL 4

Expert Comment

by:tomook
ID: 1975946
Well, deighton's answer runs in O(n) time, where n is the days between the dates. The one I posted runs in constant time. Almost always better, my CSci prof used to say.
0
 
LVL 4

Expert Comment

by:tomook
ID: 1975947
Well, deighton's answer runs in O(n) time, where n is the days between the dates. The one I posted runs in constant time. Almost always better, my CSci prof used to say.
0
 
LVL 4

Expert Comment

by:tomook
ID: 1975948
btw, I do agree with your real point that feedback on why an answer is rejected is always appreciated.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now