# difference between dates of weeks and days

there is some function to tell me the difference between 2 dates in range of weeks and days?
or know the weeks and days difference between 2 dates.

thanks for the help

It’s easy enough to write your own with a little bit of Integer math and the TotalSeconds property of the Date class:

[code]Function WeeksSince(SomeDay As Date) As String
Const secondsinday = 86400

’ the date to compare SomeDay to
Dim Now As New Date

’ the number of days between the dates
Dim totaldays As Integer = Abs((Now.TotalSeconds - SomeDay.TotalSeconds) \ secondsinday)

’ the number of whole weeks between the dates
Dim totalweeks As Integer = totaldays \ 7

’ the number of days remaining
Dim remainingdays As Integer = totaldays Mod 7

If Now.TotalSeconds > SomeDay.TotalSeconds Then ’ SomeDay is in the past
Return Str(totalweeks) + " weeks and " + Str(remainingdays) + " days ago."
Else ’ SomeDay is in the future
Return Str(totalweeks) + " weeks and " + Str(remainingdays) + " days from now."
End If

End Function
[/code]

Andrew, that function will “fail” (depending on what is expected) in some cases. If the dates that are compared correspond to two consecutive days, 1 PM on the first day and 11 AM on the second one, I would want the function to return a difference of one day, whereas as it is now it will return 0 days.

You could compare the time of the two dates, and add one day to the result if SomeDay was sometime in the past and its time is later (later? larger?) and also add one day if SomeDay is a future date having an earlier time.

Julen

You could grab the code I posted years ago for Date Deltas

Correct, the above code only considers a full 24-hour period to be one day. Periods shorter than 24 hours are discarded. If this method returns 0, then the difference is less-than one day; if the method returns 1 then the difference is greater-than (or exactly) one, but less-than two days, and so on. In the end, I suppose it’s mostly a matter of personal coding style and design needs.

Sure, just trying to help the OP in case he expects the behavior I proposed.

Julen

great, running the calculation of days and weeks.

Try to WeekOfYear and dayofyear, to make a difference regarding the dates compared with these parameters,
but I only worked for 1 year difference.

Andrew in the code, for negative values??, we know that they are correct and you just have to make absolute (positive)
and simply multiply by -1.

thanks for the help