|
-
Feb 5th, 2002, 01:32 PM
#1
Thread Starter
Hyperactive Member
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]
-
Feb 5th, 2002, 01:52 PM
#2
Frenzied Member
VB Code:
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
-
Feb 5th, 2002, 02:14 PM
#3
Member
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
-
Feb 5th, 2002, 02:30 PM
#4
Thread Starter
Hyperactive Member
Take a look at this.. this is what I came up with:
VB Code:
Public Function NewWeek(dtDate1 As Date, dtDate2 As Date) As Boolean
Dim dtDate1FirstDayOfWeek, dtDate2FirstDayOfWeek As Date
' get the first day of the week for each date
dtDate1FirstDayOfWeek = dtDate1 - (Weekday(dtDate1, vbMonday) - 1)
dtDate2FirstDayOfWeek = dtDate2 - (Weekday(dtDate2, vbMonday) - 1)
'compare
If dtDate1FirstDayOfWeek = dtDate2FirstDayOfWeek Then
NewWeek = False
Else
NewWeek = True
End If
End Function
what do you think? can you tweak this?
-mcd
[vbcode]
'*****************************
MsgBox "MCD :: [email protected]", vbInformation + vbOKOnly, "User"
'*****************************
[/vbcode]
-
Feb 5th, 2002, 06:51 PM
#5
Member
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.
-
Feb 6th, 2002, 02:41 PM
#6
Thread Starter
Hyperactive Member
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]
-
Feb 6th, 2002, 03:37 PM
#7
Thread Starter
Hyperactive Member
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:
Public Function NewWeek(dtDate1 As Date, dtDate2 As Date) As Boolean
NewWeek = False
if DateDiff("ww", Date1, Date2) > 0 then NewWeek = True
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|