Results 1 to 7 of 7

Thread: 2 dates, different week? (resolved)

  1. #1

    Thread Starter
    Hyperactive Member MetallicaD's Avatar
    Join Date
    Feb 2001
    Location
    Tallahassee, FL
    Posts
    488

    2 dates, different week? (resolved)

    Hey there.. i need a nifty little function that, if given 2 dates, will tell me true or false if they are from a different week.

    Thanks,
    -mcd
    Last edited by MetallicaD; Feb 6th, 2002 at 02:46 PM.
    [vbcode]
    '*****************************
    MsgBox "MCD :: [email protected]", vbInformation + vbOKOnly, "User"
    '*****************************
    [/vbcode]

  2. #2
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    VB Code:
    1. MsgBox Format(date1, "ww", vbUseSystemDayOfWeek, vbUseSystem)
    gives you the week number of a specific date (date1 or whatever u call it)
    Instead of vbUseSystemDayOfWeek use vbMonday, vbTuesday etc to specify the first day of the week. Using the system day of week usually means sunday is the first day of the week.

    Clint

  3. #3
    Member
    Join Date
    Nov 2001
    Location
    Virginia
    Posts
    58

    how about

    Code:
    if DateDiff("ww", Date1, Date2) > 0 then
      DoEvents 'not in the same week
    End If
    This function counts the number of Sundays between the two dates. Date1 will not be counted if it falls on Sunday, but Date2 will. Hope this helps.... newty25

  4. #4

    Thread Starter
    Hyperactive Member MetallicaD's Avatar
    Join Date
    Feb 2001
    Location
    Tallahassee, FL
    Posts
    488
    Take a look at this.. this is what I came up with:

    VB Code:
    1. Public Function NewWeek(dtDate1 As Date, dtDate2 As Date) As Boolean
    2.     Dim dtDate1FirstDayOfWeek, dtDate2FirstDayOfWeek As Date
    3.    
    4.     ' get the first day of the week for each date
    5.     dtDate1FirstDayOfWeek = dtDate1 - (Weekday(dtDate1, vbMonday) - 1)
    6.     dtDate2FirstDayOfWeek = dtDate2 - (Weekday(dtDate2, vbMonday) - 1)
    7.    
    8.     'compare
    9.     If dtDate1FirstDayOfWeek = dtDate2FirstDayOfWeek Then
    10.         NewWeek = False
    11.     Else
    12.         NewWeek = True
    13.     End If
    14.    
    15. End Function

    what do you think? can you tweak this?
    -mcd
    [vbcode]
    '*****************************
    MsgBox "MCD :: [email protected]", vbInformation + vbOKOnly, "User"
    '*****************************
    [/vbcode]

  5. #5
    Member
    Join Date
    Nov 2001
    Location
    Virginia
    Posts
    58
    Either I don't understand what you need done, or you're
    working too hard to solve a simple problem... this does the
    same thing as your previous code example.

    Code:
    Public Function NewWeek(dtDate1 As Date, dtDate2 As Date) As Boolean
    
       NewWeek = False
       if DateDiff("ww", Date1, Date2) > 0 then NewWeek = True
    
    End Function
    In only two lines... wow! Just think of the room you'll be saving!
    Work smarter, not harder.

  6. #6

    Thread Starter
    Hyperactive Member MetallicaD's Avatar
    Join Date
    Feb 2001
    Location
    Tallahassee, FL
    Posts
    488
    newty25, you have your head on straight! Thanks.. i appreciate the simplified code, works great and in less steps than mine!

    -mcd
    [vbcode]
    '*****************************
    MsgBox "MCD :: [email protected]", vbInformation + vbOKOnly, "User"
    '*****************************
    [/vbcode]

  7. #7

    Thread Starter
    Hyperactive Member MetallicaD's Avatar
    Join Date
    Feb 2001
    Location
    Tallahassee, FL
    Posts
    488
    Hey there.. some modifications.. the code originally posted would not work if date 2 was before date 1.. it would always return true in those cases.. Also, some of the variable names were incorrect in newty35's code

    VB Code:
    1. Public Function NewWeek(dtDate1 As Date, dtDate2 As Date) As Boolean
    2.  
    3.    NewWeek = False
    4.    if DateDiff("ww", Date1, Date2) > 0 then NewWeek = True
    5.  
    6. End Function

    -mcd
    [vbcode]
    '*****************************
    MsgBox "MCD :: [email protected]", vbInformation + vbOKOnly, "User"
    '*****************************
    [/vbcode]

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