Solved

DateDiff without Sunday

Posted on 1998-06-29
7
537 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

631 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