Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dates & Times 2

Status
Not open for further replies.

anastasia

Programmer
Dec 13, 2000
111
GB
Hi, I have the following code:

<CFSET tommorow = DateAdd(d,1,Now())>

<CFIF #bookdate# EQ #tommorow# AND Time() LT &quot;20:00&quot;>
Error!
</CFIF>

With this code what I am trying to do is set the variable tommorow to tommorows system date by using the DateAdd function by adding a day d, one day with the 1 to the system date Now().

Then I carry out a CFIF statement which compares a date entered by a user within an input box in WML and if this is equal to the variable tommorow and the system Time() is less than 20:00 hours then the user cannot make a booking.
So if a user is booking for a date tommorow but the time of booking is before 20:00 hours they cannot.

I have a feeling that the code above is not right, I am not sure about the DateAdd function?. I have implemented the code but does not work.

Any help appreciated.
 
HI,

The first, and only, thing I noticed that I would suspect is putting quotes around the D , and pound signs around the function like this.

<CFSET tommorow = #DateAdd('d',1,Now())#>

let me know if that fixes it
 
Hey Anastasia,

tlhawkins is right, you'll need quotes around the D in your dateAdd() function. If bookDate is a text string entered by the user, there's one other item you'll need to fix. The variable #tomorrow# will contain a value similar to this &quot;{ts '2001-04-03 20:42:44'}&quot;. If you compare it to something the visitor entered like &quot;4/3/2001&quot;, the <cfif> will never recognize these two strings as being equal. You'll need to format these as dates with dateFormat so that they will truly be the same for identical dates. The dateCompare() function is really ideal for this but since you are creating tomorrow by using now() as a starting time, it will contain a date part and a time part. This means that dateCompare won't consider them equal unless bookDate also contains a time part and they both match down to the second. Therefore, just use dateFormat like this and you should get an accurate comparison.

<cfif dateFormat(bookDate,&quot;mm/dd/yyyy&quot;) is dateFormat(tomorrow,&quot;mm/dd/yyyy&quot;)>
....
</cfif>

Also, I don't recognize the time() function so I assume this is a custom tag but I suspect you may have similar trouble comparing its output to a string containing &quot;20:00&quot;.

Hope this helps,
GJ
 
Thanks tlhawkins/GunJack I have implemented your code and everything works fine with the formating. The code is as follows and works a treat:

<CFSET tommorow = #DateAdd('d',1,Now())#>
<CFSET bookdate = DateFormat(bookdate,&quot;mm/dd/yy&quot;)>
<CFSET tommorow = DateFormat(tommorow,&quot;mm/dd/yy&quot;)>
<CFSET time = Now()>
<CFSET time = TimeFormat(time,&quot;HH:mm&quot;)>
<CFSET endtime = '20:00'>
<CFSET endtime = TimeFormat(endtime,&quot;HH:mm&quot;)>

<CFIF #bookdate# EQ #tommorow# AND #time# GT #endtime#>
<cflocation url=&quot;Options.cfm?errMsg=Tommorow##add&quot;>
</CFIF>

 
Anastasia, see my post in the other thread about the date comparison. The thing you are trying to do is tricky!


<webguru>iqof188</webguru>
 
Hey IQ,

The datecompare won't work for her in this case. See my earlier post on this thread for the reason why.

So where've you been lately?

GJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top