write datediff value inside of statement? (Full Version)

All Forums >> [Web Development] >> ASP and Database



Message


AllenD -> 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 -> 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




AllenD -> 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"
--------------------------------------------^








AllenD -> 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.





AllenD -> 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?





daveh42 -> 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




AllenD -> 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?





daveh42 -> RE: write datediff value inside of statement? (1/25/2002 10:45:54)

Does it work without the CStr?





AllenD -> 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%>






daveh42 -> 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
%>






AllenD -> 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...?





daveh42 -> 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






AllenD -> 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!





Spooky -> 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




CoolDre -> 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?




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.140625