سؤال

I have a table date range that i'm retrieving with one date specified x_VisitDate

And four variables defined that will use the x_VisitDate

For this instance x_VisitDate = 22 May 2014 the download button should only be available 10 days prior to the given date as at today we are 7 days away.

x_TodaysDate
x_DaysBeForeToDownloadFile = 10
x_AdditionalDayToDownloadFile = 1

x_CanDownloadFile = DateAdd("d", x_DaysBeForeToDownloadFile , x_VisitDate)
x_ExtraCanDownloadFile = DateAdd("d", x_AdditionalDayToDownloadFile, x_VisitDate)

I'm currently writing the following

If x_CanDownloadFile <= x_TodaysDate then

and it's showing the file when the date is set for 21 Oct 2018 (Future) and 23 Feb 2014 (past)

I'm going crazy here...

thanks

هل كانت مفيدة؟

المحلول

Don't use comparisons to constructed limits, but DateDiff for range checks. It's much easier to get right. Demo code:

>> nDaysAvailable = 3
>> dtToday = Date
>> For i = -5 To 0
>>     dtCheck = DateAdd("d", i, dtToday)
>>     nDiff   = DateDiff("d", dtCheck, dtToday)
>>     WScript.Echo dtCheck, nDiff, CStr(nDiff < nDaysAvailable)
>> Next
>>
10.05.2014 5 False
11.05.2014 4 False
12.05.2014 3 False
13.05.2014 2 True
14.05.2014 1 True
15.05.2014 0 True

(german locale)

نصائح أخرى

Maybe I'm confused by your question but can't you just do this?

x_VisitDate = #5/22/14#

If Date >= x_VisitDate - 10 And Date <= x_VisitDate + 1 Then
    ' Show file
End If

Also, you don' t need to use DateDiff() when you're working with days. Just use integer math.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top