?
Solved

VBScript DateDiff in ASP

Posted on 2003-03-26
10
Medium Priority
?
870 Views
Last Modified: 2012-08-14
I need to return the age of a person on the client side as soon as they tab or click on the next field. I wrote this little script but I get a object required error. I'm pulling the effective date from a database field.

<%
Dim EffeDate, DaysDiff, YearDiff, pday, bday
 
EffeDate = Session("EffDate")

If EffeDate = "" Then
     EffeDate = cleffdate
Else
     EffeDate = EffeDate
End If
     
Function checkage()
     bday = DOB.Value
     Response.Write bday
    pday = CDate(EffeDate)
    DaysDiff = DateDiff("d", bday, pday)
    YearDiff = DaysDiff / 365.25
    AgeFunction = YearDiff
    calcedAge = AgeFunction
End Function
%>



<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<table>
<tr><tr>
<td><b>Date of Birth:</b></td>
<td><input id="text3" name="DOB" size="23" style="WIDTH: 166px; HEIGHT: 22px" onBlur="return checkage()"></td>
</tr><tr>
<td><b>Age:</b></td>
<td>
<input id="text4" name="Age" size="6" style="WIDTH: 46px; HEIGHT: 22px" maxlength="5" value="<%=calcedAge%>"></td>
               </tr><tr>
<P>&nbsp;</P>

</BODY>
</HTML>
0
Comment
Question by:HWilfong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 2
10 Comments
 
LVL 3

Expert Comment

by:ExcelRay
ID: 8212827
You cannot call a server-side function from the client like this.  You'd have to hit the server again to run an active SERVER page function.

Ray at work
0
 
LVL 6

Expert Comment

by:thomasdodds
ID: 8213398
how is your date in the session variable formatted? I will show you a js client side function, unless you want to submit the page to get the value of checkage() ...
0
 
LVL 6

Expert Comment

by:thomasdodds
ID: 8213450
or this might do you alright (if you are going to submit the form to calculate the age):

<%
Dim EffeDate, DaysDiff, YearDiff, pday, bday
 
EffeDate = Session("EffDate")

If EffeDate = "" Then
    EffeDate = cleffdate
Else
    EffeDate = EffeDate
End If
     
Function checkage()
  Dim varAge = DateDiff("d", CDate(Request.Form("DOB")), CDate(EffeDate)) / 365.25
 
  If Not IsNull(varAge) and Not varAge = 0 then
     Return varAge
  Else
     Return ""
  End If
   
End Function
%>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<form id="myform" method="post">
<table ID="Table1">
<tr><tr>
<td><b>Date of Birth:</b></td>
<td><input id="text3" name="DOB" size="23" style="WIDTH: 166px; HEIGHT: 22px" onBlur="document.forms[0].submit();"></td>
</tr><tr>
<td><b>Age:</b></td>
<td>
<input id="text4" name="Age" size="6" style="WIDTH: 46px; HEIGHT: 22px" maxlength="5" value="<%=checkage()%>"></td>
              </tr><tr>
<P>&nbsp;</P>
</form>
</BODY>
</HTML>
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 6

Expert Comment

by:thomasdodds
ID: 8213473
sorry:

or this might do you alright (if you are going to submit the form to calculate the age):

<%
Dim EffeDate
Dim varAge

EffeDate = Session("EffDate")

If EffeDate = "" Then
   EffeDate = cleffdate
Else
   EffeDate = EffeDate
End If
   
Call checkage()

Sub checkage()
 varAge = DateDiff("d", CDate(Request.Form("DOB")), CDate(EffeDate)) / 365.25
 
 If IsNull(varAge) Or varAge = 0 then
    varAge = ""
 End If
 
End Sub
%>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<form id="myform" method="post">
<table ID="Table1">
<tr><tr>
<td><b>Date of Birth:</b></td>
<td><input id="text3" name="DOB" size="23" style="WIDTH: 166px; HEIGHT: 22px" onBlur="document.forms[0].submit();"></td>
</tr><tr>
<td><b>Age:</b></td>
<td>
<input id="text4" name="Age" size="6" style="WIDTH: 46px; HEIGHT: 22px" maxlength="5" value="<%=varAge%>"></td>
             </tr><tr>
<P>&nbsp;</P>
</form>
</BODY>
</HTML>
0
 
LVL 6

Expert Comment

by:thomasdodds
ID: 8213490
and if you get errors in the sub due to no value in Request.Form("DOB"):

Sub checkage()
    If Not Request.Form("DOB") = "" Then
        varAge = DateDiff("d", CDate(Request.Form("DOB")), CDate(EffeDate)) / 365.25

        If IsNull(varAge) Or varAge = 0 then
           varAge = ""
        End If
    End If
End Sub
0
 

Author Comment

by:HWilfong
ID: 8217598
Hello Thomasdodds,
The date format is mm/dd/yyyy, and I do need it to be client side. The project I'm working on is for an insurance company and in order for them to give the proper qoutes they need to see the age of the insured after they enter the birth date. I'm new to ASP or to any web development. I read about the onBlur and would like to take advantage of any type of event handler so the user gets the information they need after tabbing or clicking out of the text area without submitting the form.
Many thanks  
0
 
LVL 6

Accepted Solution

by:
thomasdodds earned 1000 total points
ID: 8218047
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<script language="javascript">
<!--
   function checkage(paramDestField){
      var curDate = new Date(); //current date
      var tmpDate = new String(document.frms[0].DOB.value);
      var arrDateParts;

   if (tmpDate != '') {// only do if you have a value to work with
      arrDateParts = tmpDate.split('/'); // mm/dd/yyyy

      var dobDate = new Date(parseInt(arrDateParts[2]),parseInt(arrDateParts[0])-1, parseInt(arrDateParts[1])); // inputs: year_num, month_num (0 based index), day_num

      var diffMilliSecs = curDate - dobDate; /// js: date - date = millisecs
      var diffYears = new String(diffMilliSecs/31449600000); // get the difference in years from millisecs
      var point = diffYears.indexOf(".")    //find the decimal point
      var objField = eval('document.forms[0].' + paramDestField); // build reference to destination field passed in
      objField.value = diffYears.substring(0,point); // stuff whole value into field
   }
   }
//-->
</script>
</HEAD>
<BODY>
<form id="myform" method="post">
<table ID="Table1">
<tr><tr>
<td><b>Date of Birth:</b></td>
<td><input id="text3" name="DOB" size="23" style="WIDTH: 166px; HEIGHT: 22px" onBlur="checkage();"></td>
</tr><tr>
<td><b>Age:</b></td>
<td>
<input id="text4" name="Age" size="6" style="WIDTH: 46px; HEIGHT: 22px" maxlength="5" value="<%=varAge%>"></td>
            </tr><tr>
<P>&nbsp;</P>
</form>
</BODY>
</HTML>
0
 
LVL 6

Expert Comment

by:thomasdodds
ID: 8218054
forgot to change

><input id="text3" name="DOB" size="23" style="WIDTH: 166px; HEIGHT: 22px" onBlur="checkage();">

to


><input id="text3" name="DOB" size="23" style="WIDTH: 166px; HEIGHT: 22px" onBlur="checkage('Age');">
0
 

Author Comment

by:HWilfong
ID: 8220732
Thank you very much thomasdodds.
Excellent!
0
 
LVL 6

Expert Comment

by:thomasdodds
ID: 8224608
You are very welcome!

I have found the following to be of great help:

http://www.devguru.com
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question