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
Printable View
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
gives you the week number of a specific date (date1 or whatever u call it)VB Code:
MsgBox Format(date1, "ww", vbUseSystemDayOfWeek, vbUseSystem)
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
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.... newty25Code:if DateDiff("ww", Date1, Date2) > 0 then
DoEvents 'not in the same week
End If
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
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.
In only two lines... wow! Just think of the room you'll be saving!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
Work smarter, not harder.
newty25, you have your head on straight! Thanks.. i appreciate the simplified code, works great and in less steps than mine!
-mcd
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