|
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
|
|
|
January 15th, 2006, 11:14 PM
|
Registered User
|
|
Join Date: Jan 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
help with DateDiff
hello i am making a little user input form for a class i have that finds how old you are in years, months, and days.
I know you use this function
dateDiff("yyyy", request("strBirthDate"), date())
but i have no idea how to use it i know that the last date is the current date while the strBirthdate is the birthdate of someone who inputs thier birthdate in xx/xx/xxxx format.
the form looks like this
<form>
<p>Please enter your birthday in the format mm/dd/yy:
<input type="text" name="strBirthDate">
<input type="submit" value="Calculate my age"></p>
</form>
i want the output to be like this
you are x years old
you are x months old
you are x days old
prior thanx to whoever helps i know this is an easy one but hey im new to this stuff.
|
January 15th, 2006, 11:53 PM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
;;but i have no idea how to use it
The DateDiff Function Returns the number of intervals between two dates.
So get the users birthdate (submit the form) then in the head of that page do something like:
<%
'hard coded date for testing - just replace bDate with request.form("fieldName")
dim bDate
bDate = cDate("27/10/1972")
response.write "You are " & dateDiff("yyyy", bDate, date()) & " years old<br>You are " & dateDiff("m", bDate, date()) & " months old<br>You are " & dateDiff("d", bDate, date()) & " days old"
%>
Wind is your friend
Matt
|
January 16th, 2006, 12:20 AM
|
Registered User
|
|
Join Date: Jan 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanx but now i have a type mismatch problem
ok my code looks like this
<% Option Explicit %>
<% dim strBirthdate %>
<% dim DateDiff%>
<html>
<head>
<title>Age Calculator</title>
</head>
<center>
<h2>Age Calculator</h2>
<center>
<form>
<p>Please enter your birthday in the format mm/dd/yy:
<input type="text" name="strBirthDate">
<input type="submit" value="Calculate my age"></p>
</form>
<%
'hard coded date for testing - just replace bDate with request.form("strBirthDate")
response.write("You are " & dateDiff("yyyy", strBirthDate, date()) & " years old<br>")
response.write("You are " & dateDiff("m", strBirthDate, date()) & " months old<br>")
response.write("You are " & dateDiff("d", strBirthDate, date()) & " days old<br>")
%>
</html>
how do i get rid of this error thanx
|
January 16th, 2006, 12:31 AM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
Help other help you. What line is your error, please also illustrate which line in your code.
NOTE :
1..You do not need to dim dateDiff this is a function and should not be used as anything else
2..You use strBirthDate as the date variable, where have you assigned the form value submitted to this variable?
3..where is the rest of your <form> tag properties? action, method etc
Did my example work when the date was hard coded? It did for me, the work is done for you. All you need to do is substitute my hard coded date for your form variable:
Replace:
("You are " & dateDiff("yyyy", strBirthDate,...
With with:
("You are " & dateDiff("yyyy", trim(request.form("strBirthDate")),...
Wind is your friend
Matt
|
January 16th, 2006, 12:32 AM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
Also where are your <body> tags
Wind is your friend
Matt
|
January 16th, 2006, 12:38 AM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
While I have bugger all to do:
The method you use asking for thier bDate is poor. Place three <select> combo boxes to eliminate the need for MASSIVE validation (or a date picker - this is what I use). A free text area for a date means it could come in any format. The dateDiff function requires a valid date format that is a date data type. Notice I used the cDate function in my post, reasearch this. Also look at the isDate function.
Thre boxes, one for 1-31 days, one for jan-Dec and one for the years. There are lots of free code examples out there for this
Wind is your friend
Matt
|
January 16th, 2006, 01:13 AM
|
Registered User
|
|
Join Date: Jan 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hey thanx for all your help so far the error is
Type mismatch: '[string: ""]'
line 18
<% Option Explicit %>
<% dim bDate %>
<html>
<head>
<title>Age Calculator</title>
</head>
<body>
<center>
<h2>Age Calculator</h2>
<center>
<form>
<p>Please enter your birthday in the format mm/dd/yyyy:
<input type="text" name="bDate">
<input type="submit" value="Calculate my age"></p>
</form>
<%
Response.Write("You are " & dateDiff("yyyy", trim(request.form("bDate")), date()) & "years old<br>")
Response.Write("You are " & DateDiff("m", trim(request.form("bDate")), date()) & "months old<br>")
Response.Write("You are " & DateDiff("d", trim(request.form("bDate")), date()) & "days old<br>")
%>
</body>
</html>
doesnt that mean that i am showing a string name that doesnt match with the stated one i dont understand.
thanx
|
January 16th, 2006, 01:40 AM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
mmmmm. Lets suppose you had 300 lines of code, do you expect people to count your lines. You dont, however pointing out the problematic line number AND LINE is what you should do for a prompt solution. Also what I asked for:
;;;Help other help you. What line is your error, please also illustrate which line in your code.
Anyhow your problem is the date format because when I hard code a date it works fine - cut n paste working hard coded version:
<%
dim dVal
dVal = "27/10/1972"
Response.Write("You are " & dateDiff("yyyy", dVal, date()) & "years old<br>")
Response.Write("You are " & DateDiff("m", dVal, date()) & "months old<br>")
Response.Write("You are " & DateDiff("d", dVal, date()) & "days old<br>")
%>
Comment out your code and write the date value you are using to the browser. EG:
<%
response.write trim(request.form("bDate")) & " =date value being passed"
'Response.Write("You are " & dateDiff("yyyy", trim(request.form("bDate")), date()) & "years old<br>")
'Response.Write("You are " & DateDiff("m", trim(request.form("bDate")), date()) & "months old<br>")
'Response.Write("You are " & DateDiff("d", trim(request.form("bDate")), date()) & "days old<br>")
%>
1..Has the form object got a value? Post this value
2..is it a date data type:
response.write isDate(trim(request.form("bDate")))
If it prints false to the browser it is not a date, therefore use the cDate function to convert it (The two functions I told you to check out)
Wind is your friend
Matt
|
January 16th, 2006, 01:56 AM
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 90
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The reason you have the error is because there is no validation. So when you access the page the first time no date has been entered and you are trying to manipulate a variable that is no a valid date.
If you are calling the result on the same page use a hidden field which triggers the calculation of years, days and dates. See my code below.
Also you will need to validate that the user has entered a valid date as well. Use the IsDate() Function
Lastly your from method needs to be set to "post" to use Request.Form
<% Option Explicit %>
<% dim bDate, cmd %>
<html>
<head>
<title>Age Calculator</title>
</head>
<body>
<center>
<h2>Age Calculator</h2>
<center>
<form method="post">
<p>Please enter your birthday in the format mm/dd/yyyy:
<input type="text" name="bDate">
<input type="submit" value="Calculate my age">
<input name="cmd" type="hidden" id="cmd" value="calculate">
</p>
</form>
<%
cmd = Request.Form("cmd") ' Form trigger to calculate date diff's
If cmd = "calculate" Then
' Validate Date is Valid
If IsDate(request.form("bDate")) Then
Response.Write("You are " & dateDiff("yyyy", trim(request.form("bDate")), date()) & "years old<br>")
Response.Write("You are " & DateDiff("m", trim(request.form("bDate")), date()) & "months old<br>")
Response.Write("You are " & DateDiff("d", trim(request.form("bDate")), date()) & "days old<br>")
Else
Response.Write "Error: Please enter a valid date. (mm/dd/yyyy)"
End If
End If
%>
</body>
</html>
TDA
|
January 16th, 2006, 02:34 AM
|
Registered User
|
|
Join Date: Jan 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hey thanx for helping me out i understand what i was doing wrong, im just not totally up on synatx yet thanx again
|
Similar Threads
|
Thread |
Thread Starter |
Forum |
Replies |
Last Post |
DateDiff |
zahyea |
.NET Framework 1.x |
0 |
October 16th, 2007 04:38 AM |
DateDiff Question |
ExDb |
BOOK: Beginning VB.NET Databases |
2 |
August 25th, 2007 10:11 PM |
datediff |
ebburks |
Access |
2 |
July 28th, 2006 06:58 PM |
DateDiff?? ( w/o weekends... ) |
underscore10304 |
Access VBA |
7 |
January 24th, 2006 08:55 AM |
Using DateDiff |
iancrabtree |
Access |
2 |
November 27th, 2005 07:33 PM |
|
|