...

View Full Version : IF Then With Date



Crash1hd
01-28-2003, 08:09 AM
I am having trouble with the following code!

YearField = Year(Date)

If YearField = RS.fields("DoFYearField") Then
response.write "Current Date " & RS.fields("DoFYearField") & "<BR>" & (YearField) & "<br>"
Else
response.write "Not Current Date " & RS.fields("DoFYearField") & "<BR>" & (YearField) & "<br>"
End If

and I make sure that the current is equal but I always get Not Current Date as the output?

What am I doing wrong!

raven
01-28-2003, 09:22 AM
Two things to check first:

Change - YearField = Year(Date)
to - YearField = Year(Date())


If it's still not working then add a CInt to ensure data types are compatible, thus:

YearField = CInt(Year(Date()) )

If YearField = CInt(RS.fields("DoFYearField")) Then
response.write "Current Date " & RS.fields("DoFYearField") & "<BR>" & (YearField) & "<br>"
Else
response.write "Not Current Date " & RS.fields("DoFYearField") & "<BR>" & (YearField) & "<br>"
End If

Crash1hd
01-28-2003, 09:55 AM
So all I really was missing was the CInt :)

Lol

Thankyou Very much

Ok Next step what I am trying to achieve is that if the current date is before the event date it will post when it is if it is past the event date then it will show sorry this event has passed or something like that I can get it to check the year and month but once I try to get it to check the day it doesnt post properly if it is the 15th of january and the event happend on the 14th of febuary if I add the day field it says sorry it has already happend! even though it hasnt!

Any help :)

Thanks in advance

raven
01-28-2003, 10:10 AM
If that makes it work, then yes!

I always use date() or now() for date/time, so not sure of date works without the parentheses.

Crash1hd
01-28-2003, 11:13 AM
So the following is what I have


If YearField =< CInt(RS.fields("DoFYearField")) Then
If MonthField =< CInt(RS.fields("DoFMonthField")) Then

response.write "Current Date 0" & (MonthField) & " " & (DayField) & " " & (YearField) & "<br>Event is on " & RS.fields("DoFMonthField") & " " & RS.fields("DoFDayField") & " " & RS.fields("DoFYearField") & "<br>"
Else
response.write "Current Date 0" & (MonthField) & " " & (DayField) & " " & (YearField) & "<br>Event was on " & RS.fields("DoFMonthField") & " " & RS.fields("DoFDayField") & " " & RS.fields("DoFYearField") & "<br>"

End If
End If

Of course this works for the following month but if its the current month and the day has passed then it still says the first cause how do you check for day only when month equals current month! once I figure that out I will also be adding TIme down to the min?

:)

raven
01-28-2003, 11:28 AM
I believe you'll have to add an AND test to:

If MonthField =< CInt(RS.fields("DoFMonthField")) Then


So you will have:

If MonthField =< CInt(RS.fields("DoFMonthField")) AND {and test here} Then

Crash1hd
01-28-2003, 11:38 AM
Doh I had thought of useing The AND feature but I have been working with javascript to long lol cause I tried to use & and && and niether of them worked!


Thankyou verymuch again! :)

P.s. Do you know of a place that has a good reference to the different possible features do and mean for example

= is equals to
<> is not equals to
> greater then
< less then
& I thought was and but I gues AND is and
| is or but I may be wrong I need something that list all of the possibilitys like the one I have for javascripts!

at

http://www.devguru.com/Technologies/ecmascript/quickref/javascript_index.html

raven
01-28-2003, 11:45 AM
The {} were just to indicate where to insert the test.

If MonthField = CInt(RS.fields("DoFMonthField")) And DayField = CInt(RS.fields("DoFDayField")) Then

Should work.

Crash1hd
01-28-2003, 11:49 AM
Actually I had to do the following

If MonthField = CInt(RS.fields("DoFMonthField")) AND (DayField =< CInt(RS.fields("DoFDayField"))) Then

To get it to work I had to put () around it lol :) again if you can check the post above yours above this one as I had changed it while you where replying :)

Thankyou Very much

Adam

P.s. Is there a or statement or an opposite to and

Crash1hd
01-28-2003, 12:31 PM
Ok So I put the following as the code

If YearField =< CInt(RS.fields("DoFYearField")) Then
If MonthField =< CInt(RS.fields("DoFMonthField")) OR MonthField = CInt(RS.fields("DoFMonthField")) AND (DayField =< CInt(RS.fields("DoFDayField"))) Then

response.write "Current Date 0" & (MonthField) & " " & (DayField) & " " & (YearField) & "<br>Event is on " & RS.fields("DoFMonthField") & " " & RS.fields("DoFDayField") & " " & RS.fields("DoFYearField") & "<br>"
Else
response.write "Current Date 0" & (MonthField) & " " & (DayField) & " " & (YearField) & "<br>Event was on " & RS.fields("DoFMonthField") & " " & RS.fields("DoFDayField") & " " & RS.fields("DoFYearField") & "<br>"

End If
End If

But useing that feature alows when its passed it still says is on???

Crash1hd
01-28-2003, 12:42 PM
Doh I finaly figured it out lol figures I always figure it out after awhile :)

here it is

If YearField => CInt(RS.fields("DoFYearField")) Then
If MonthField < CInt(RS.fields("DoFMonthField")) Then
response.write "Current Date 0" & (MonthField) & " " & (DayField) & " " & (YearField) & "<br>Event is on " & RS.fields("DoFMonthField") & " " & RS.fields("DoFDayField") & " " & RS.fields("DoFYearField") & "<br>"
Else
If MonthField = CInt(RS.fields("DoFMonthField")) AND (DayField =< CInt(RS.fields("DoFDayField"))) Then

response.write "Current Date 0" & (MonthField) & " " & (DayField) & " " & (YearField) & "<br>Event is on " & RS.fields("DoFMonthField") & " " & RS.fields("DoFDayField") & " " & RS.fields("DoFYearField") & "<br>"
Else
response.write "Current Date 0" & (MonthField) & " " & (DayField) & " " & (YearField) & "<br>Event was on " & RS.fields("DoFMonthField") & " " & RS.fields("DoFDayField") & " " & RS.fields("DoFYearField") & "<br>"
End If
End If
End If

raven
01-28-2003, 12:51 PM
Glad it's working. There 's an OR operator, in your context it would be used as:

If condition1 OR condition2 then

whammy
01-29-2003, 01:59 AM
Whenever you're trying to get the Difference between dates, VBScript (and SQL!) have built in functions called DateDiff() which will solve this problem for you. I use it all the time. ;) As a matter of fact, using that will probably shorten your code considerably.

Have a search on google, you will find the relevant information on the following sites as well:

http://www.w3schools.com/vbscript/func_datediff.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsfctdatediff.asp

Believe me, it's much easier than the method that you're currently employing (although I see no reason why it won't work - but that's beside the point).

:)

raven
01-29-2003, 07:51 AM
I aslo use datediff() with great frequency, but was just using the method above to help solve the specific problem.:)

Crash1hd
01-30-2003, 11:36 AM
Thanks for all the info I will check out the DateDiff() function for sure! maybe if someone where to post a small example it would make more sense quicker :)

Thanks again

raven
01-30-2003, 12:00 PM
It is explained in full here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsfctDateDiff.asp



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum