navigation
a webmaster learning community
     Home    Register     Search      Help      Login    
Sponsors

Shopping Cart Software
Ecommerce software integrated into Frontpage, Dreamweaver and Golive templates. No monthly fees and available in ASP and PHP versions.

Website Templates
We also have a wide selection of Dreamweaver, Expression Web and Frontpage templates as well as webmaster tools and CSS layouts.

Frontpage website templates
Creative Website Templates for FrontPage, Dreamweaver, Flash, SwishMax

Search Forums
 

Advanced search
Recent Posts

 Todays Posts
 Most Active posts
 Posts since last visit
 My Recent Posts
 Mark posts read

Microsoft MVP

 

write datediff value inside of statement?

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
Printable Version 

All Forums >> Web Development >> ASP and Database >> write datediff value inside of statement?
Page: [1]
 
AllenD

 

Posts: 258
From: Dayton, OH, USA
Status: offline

 
write datediff value inside of statement? - 1/24/2002 13:42:09   
Hello all, I have a statement that will calculate 2 dates and write the value and the word day or days...but the problem is that if it is zero days I do not want to write the number 0. How or where can I put the <%=DateDiff("D",tdate,backdate)%> statement if it equals anything but zero?

Here is my code without writing the value of datediff because I don't know how to include it inside of the else statement...

<%If DateDiff("D",tdate,backdate)>1 then response.write "days" Else If DateDiff("D",tdate,backdate)=1 then response.write "day" Else If DateDiff("D",tdate,backdate)=0 then response.write "Today"%>


Spooky

 

Posts: 26597
Joined: 11/11/1998
From: Middle Earth
Status: offline

 
RE: write datediff value inside of statement? - 1/24/2002 15:45:26   
You may want to step backwards in the code, and check the result first, then decide if you want to write the day.

eg :

Select Case DateDiff("D",tdate,backdate)

Case 0
response.write "Today"
Case 1
response.write "1 day"
Case ELSE
response.write DateDiff("D",tdate,backdate) " days"
End Select


§þððk¥
Database / DRW Q & A
VP-ASP Shopping cart
Spooky Login

(in reply to AllenD)
AllenD

 

Posts: 258
From: Dayton, OH, USA
Status: offline

 
RE: write datediff value inside of statement? - 1/24/2002 15:57:29   
I get this error Spooky
Microsoft VBScript compilation error '800a0401'

Expected end of statement

/general/ooo/outofoffice2.asp, line 130

response.write DateDiff("D",tdate,backdate) " days"
--------------------------------------------^





(in reply to AllenD)
AllenD

 

Posts: 258
From: Dayton, OH, USA
Status: offline

 
RE: write datediff value inside of statement? - 1/24/2002 16:05:32   
Today and 1 day are working, but anything higher than 1 I get the error....If I take out the DateDiff("D",tdate,backdate) it works fine, but of course the # is not being displayed...this is what I am having problems with...incorporating that line of code inside of the response.write.


(in reply to AllenD)
AllenD

 

Posts: 258
From: Dayton, OH, USA
Status: offline

 
RE: write datediff value inside of statement? - 1/24/2002 16:10:26   
Also, if the backdate is in the past, like 1/23 it is displaying the word days..is there a case statement I can add so it displays nothing?


(in reply to AllenD)
daveh42

 

Posts: 83
From: Maine USA
Status: offline

 
RE: write datediff value inside of statement? - 1/25/2002 8:36:07   
quote:
 
response.write DateDiff("D",tdate,backdate) " days"
--------------------------------------------^



The following change should work:
  Response.Write CStr(DateDiff("D",tdate,backdate)) & " days"


The "CStr" functin will convert the number of days to a string. The "&" operator is the string concatenation operator.

To display nothing for past dates, try changing the "Case Else" to:
  Case Is > 1



Hope this helps,

Dave



Edited by - daveh42 on 01/25/2002 08:38:09

(in reply to AllenD)
AllenD

 

Posts: 258
From: Dayton, OH, USA
Status: offline

 
RE: write datediff value inside of statement? - 1/25/2002 9:35:35   
Well, I dont get an error, but the number of days is not written if it is more than 1...I just get the word days...Seems the CStr doesn't work?


(in reply to AllenD)
daveh42

 

Posts: 83
From: Maine USA
Status: offline

 
RE: write datediff value inside of statement? - 1/25/2002 10:45:54   
Does it work without the CStr?


(in reply to AllenD)
AllenD

 

Posts: 258
From: Dayton, OH, USA
Status: offline

 
RE: write datediff value inside of statement? - 1/25/2002 13:00:37   
No, it does not write the #, just the word day or days. Here is my script:

<%Select Case DateDiff("D",tdate,backdate)
Case 0
Response.Write (DateDiff("D",tdate,backdate)) & " days"
Case 1
response.write "1 day"
Case ELSE
response.write " days"
End Select%>



(in reply to AllenD)
daveh42

 

Posts: 83
From: Maine USA
Status: offline

 
RE: write datediff value inside of statement? - 1/25/2002 13:15:10   
quote:
 
<%Select Case DateDiff("D",tdate,backdate)
Case 0
Response.Write (DateDiff("D",tdate,backdate)) & " days"
Case 1
response.write "1 day"
Case ELSE
response.write " days"
End Select%>



I think it should be this:
 
<%
Select Case DateDiff("D",tdate,backdate)
Case 0
Response.Write "Today"
Case 1
Response.Write "1 day"
Case Else
Response.Write (DateDiff("D",tdate,backdate)) & " days"
End Select
%>



(in reply to AllenD)
AllenD

 

Posts: 258
From: Dayton, OH, USA
Status: offline

 
RE: write datediff value inside of statement? - 1/25/2002 13:28:02   
Awesome! That worked. Now what about if the back day is in the past , it displays -1 days...How would I incoporate the code that spooky suggested above? I'm not sure how to implement it in...?


(in reply to AllenD)
daveh42

 

Posts: 83
From: Maine USA
Status: offline

 
RE: write datediff value inside of statement? - 1/25/2002 15:08:38   
quote:

Awesome! That worked. Now what about if the back day is in the past , it displays -1 days...How would I incoporate the code that spooky suggested above? I'm not sure how to implement it in...?



This should work:
 
<%
Select Case DateDiff("D",tdate,backdate)
Case 0
Response.Write "Today"
Case 1
Response.Write "1 day"
Case Else
If DateDiff("D",tdate,backdate) > 1 Then
Response.Write (DateDiff("D",tdate,backdate)) & " days"
End If
End Select
%>


Earlier I mentioned changing the "Case Else" to a "Case Is > 1", but I do not think that VBScript supports that syntax (Visual Basic does). This is why I recommend using the "If...Then...End If" statement in the "Case Else" clause. Also this should satisfy the requirement that nothing is displayed for prior dates.

While it may not make a big difference, I would also recommend storing the value of the expression "DateDiff("D",tdate,backdate)" in a variable and using that variable for comparisons. Also, the CStr function should work. Maybe something like this would work:
 
<%
Dim intDateDiff
intDateDiff = CInt(DateDiff("D",tdate,backdate))
Select Case intDateDiff
Case 0
Response.Write "Today"
Case 1
Response.Write "1 day"
Case Else
If intDateDiff > 1 Then
Response.Write CStr(intDateDiff) & " days"
End If
End Select
%>



Hope this helps,

Dave



(in reply to AllenD)
AllenD

 

Posts: 258
From: Dayton, OH, USA
Status: offline

 
RE: write datediff value inside of statement? - 1/25/2002 15:24:03   
Wow you are good! I used the second portion of code and it worked perfectly. I am pretty new at manual code, but have learned ALOT already, thanks for adding to my learning experience!


(in reply to AllenD)
Spooky

 

Posts: 26597
Joined: 11/11/1998
From: Middle Earth
Status: offline

 
RE: write datediff value inside of statement? - 1/25/2002 20:45:09   
I missed the party ;)

§þððk¥
Database / DRW Q & A
VP-ASP Shopping cart
Spooky Login

(in reply to AllenD)
CoolDre

 

Posts: 1
Joined: 10/10/2005
Status: offline

 
RE: write datediff value inside of statement? - 10/10/2005 3:01:20   
I have a similar problem. here is my code
<%
If DateDiff(m,date, rsCerts.Fields.Item("expdate").Value) > 12 then
response.write (DateDiff(yyyy,date,(rsCerts.Fields.Item("expdate").Value) & " years")
Else If DateDiff(m,date,rsCerts.Fields.Item("expdate").Value) > 0 then response.write (DateDiff(m,date,rsCerts.Fields.Item("expdate").Value & " months")
Else If DateDiff(d,date,rsCerts.Fields.Item("expdate").Value) > 0 then
response.write (DateDiff(d,date,rsCerts.Fields.Item("expdate").Value) & " days")
Else If DateDiff(d,date,rsCerts.Fields.Item("expdate").Value) < 1 then
response.write (DateDiff(h,date,rsCerts.Fields.Item("expdate").Value) & " hours")
Else
response.write ("cert has expired")
End If
%>

I cant figure out what it's doing wrong. Its not displaying anything, no error codes, nothing. Can anyone help me?

(in reply to Spooky)
Page:   [1]

All Forums >> Web Development >> ASP and Database >> write datediff value inside of statement?
Page: [1]
Jump to: 1





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts