Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 4th, 2006, 05:44 PM
Authorized User
 
Join Date: Jun 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to hcweb
Default If Then Statement With Dates

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
 
Old January 4th, 2006, 07:21 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

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
 
Old January 4th, 2006, 08:21 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

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
 
Old January 4th, 2006, 08:23 PM
Authorized User
 
Join Date: Jun 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to hcweb
Default

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
 
Old January 4th, 2006, 08:29 PM
Authorized User
 
Join Date: Jun 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to hcweb
Default

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
 
Old January 4th, 2006, 08:46 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

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
 
Old January 4th, 2006, 09:03 PM
Authorized User
 
Join Date: Jun 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to hcweb
Default

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
 
Old January 4th, 2006, 09:13 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

no worries, have a good day

Wind is your friend
Matt
 
Old January 4th, 2006, 09:21 PM
Authorized User
 
Join Date: Jun 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to hcweb
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
dates again dhoward VB.NET 2002/2003 Basics 12 August 22nd, 2007 09:48 AM
dates DARSIN General .NET 4 January 14th, 2005 09:09 AM
between dates capitala Access VBA 1 May 30th, 2004 05:20 PM
Dates treadmill SQL Language 3 July 3rd, 2003 02:32 PM
Dates oathamm Servlets 1 June 27th, 2003 05:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.