Subject: If Then Statement With Dates
Posted By: hcweb Post Date: 1/4/2006 4:44:08 PM
Hello all,
I'm having an issue in an IF THEN statement using dates.

If I do this:

If date >= #1/1/2006# AND date < #2/1/2006# Then
Do Something
Else
Do Something
End If

It works fine as it should. However, if I do this:

If date >= #1/1/" & year(now()) & "# AND date < #2/1/" & year(Now()) & "# Then
Do Something
Else
Do Something
End If

it fails and always reverts to the else portion of the statement. What exactly is going on to cause it to fail? Is it possible to do a between on the dates in and IF/Then situation?

Thanks in advance!
Chris
Reply By: mat41 Reply Date: 1/4/2006 6:21:40 PM
hcweb

What date format are you using?  We use dd/mm/yyyy here in Aussie.  This means your initial statement should be going into the else part of your code not the if.  I would write it like:

If ((date() >= cDate("1/1/2006")) AND (date() < cDate("2/1/2006"))) Then
  response.write "inside If<br>"
Else
  response.write "inside else<bR>"
End If

A couple of comments:
1..Date is a function it should be written like:
   Date()
2..IMO Drop the # method and cDate a string date value:
   cDate("1/1/2006")

   FYI: CDate Function
   Returns an expression that has been converted to a Variant
   subtype Date.

3..Use more () when comparing conditions






Wind is your friend
Matt
Reply By: mat41 Reply Date: 1/4/2006 7:21:52 PM
FYI - Your second statement:

If date >= #1/1/" & year(now()) & "# AND date < #2/1/" & year(Now()) & "# Then
Do Something
Else
Do Something
End If

Cant possibly run error free.  Are you coding with an editor that gives you colors in your code (homeSite, editPlus etc)?  If so you can tell instantly something is wrong.  If you are using notePad its more difficult.  You second condition should look like:

If ((date() >= cDate("1/1/" & year(now()))) AND (date() < "2/1/" & year(Now()))) Then
  response.write "inside If<br><bR>"
Else
  response.write "inside else<br><br>"
End If

Wind is your friend
Matt
Reply By: hcweb Reply Date: 1/4/2006 7:23:54 PM
Matt,
Thanks for the reply. I'm using English(US) m/d/yyyy. My problem is I'm having to pull info from an IBM AS/400 that does not contain penalties, and then assess the penalties on the web based on the current date compared to static dates. The months and days are always the same with only the years changing.
Example: This year I would be comparing 2/1/2006, and next year it would be 2/1/2006. I want this to automate when a new year rolls over.

BTW - I do use the Date() in the code. In my initial question I copied code I was playing with and had date instead of date().

A portion of my actual code is as follows:

If status = "unpaid" Then
If date() >= #1/1/2006# AND date() < #2/1/2006# Then
totpen = 0
printfees = 0
nettotal = totaldue + totpen + printfees
totpaid = 0
balance = nettotal - totpaid

ElseIf date() >= #2/1/2006# AND date() < #3/1/2006# Then
totpen = totaldue * .01
printfees = "0"
nettotal = totaldue + totpen + printfees
totpaid = "0"
balance = nettotal - totpaid

ElseIf date() >= #3/1/2006# AND date() < #4/1/2006# Then
totpen = totaldue * .02
printfees = "0"
nettotal = totaldue + totpen + printfees
totpaid = "0"
balance = nettotal - totpaid
(this continues on to cover the months of Apr. - Aug.)
End If
End If

I don't understand why date() >= #3/1/2006# AND date() < #4/1/2006# works and not date() >= #3/1/" & year(now()) & "# AND date() < #4/1/" & year(now()) & "# does not.

I did not know about using cdate. I've seen and read alot about using # to surround dates. Thanks for that info. Is it possible to use BETWEEN in the IF/Then?

Thanks for your help!
Chris
Reply By: hcweb Reply Date: 1/4/2006 7:29:47 PM
Matt,
I'm using Dreamweaver4 for normal, tedious layout and then handwrite all the code in the code inspector view (basically a notepad view).

Why the double parenthesis?

Chris
Reply By: mat41 Reply Date: 1/4/2006 7:46:10 PM
Have you tried:

If ((date() >= cDate("1/1/" & year(now()))) AND (date() < "2/1/" & year(Now()))) Then
  response.write "inside If<br><bR>"
Else
  response.write "inside else<br><br>"
End If

Your function cant have run as it was as mentioned in my other post.

;;;Why the double parenthesis
You cntrol the order in which things are looked at, assesd and compared.  Just like in SQL statements.

mmmm, dreamweaver does color code.  To test if a value is being compared as a date use the isDate function.  If your value is not a date it will fail.

Wind is your friend
Matt
Reply By: hcweb Reply Date: 1/4/2006 8:03:21 PM
Matt,
In my original post my first function worked fine, but the second one DID fail. Hence all the questions. I tested your code on a small basis and it worked, so now I'm editing my full function with your input. I'll let you know if that worked.

Dreamweaver4 does color code, but not errored scripting. Only on improper placement of scripting and improper HTML. It also color codes scripts, notes, html, etc, but that seems only for visual distinguishing.

I really appreciate your help as this is going to solve a major headache for me. Thanks!

Chris
Reply By: mat41 Reply Date: 1/4/2006 8:13:01 PM
no worries, have a good day

Wind is your friend
Matt
Reply By: hcweb Reply Date: 1/4/2006 8:21:23 PM
Matt,
Everything works like a champ. My original code would worked fine but, I would have had to edit the code every year. Automatically picking up the new year is definitely application friendly and requires me to do nothing now. My whole problem was getting it to read the year automatically, which you solved.

I even learned a few things. Didn't know about cdate, and have never been in a situation where I needed to use multiple parenthesis as we did here.

Thanks for all the help!!
Chris

Go to topic 34206

Return to index page 403
Return to index page 402
Return to index page 401
Return to index page 400
Return to index page 399
Return to index page 398
Return to index page 397
Return to index page 396
Return to index page 395
Return to index page 394