• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 877
  • Last Modified:

VBScript DateDiff in ASP

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
HWilfong
Asked:
HWilfong
  • 7
  • 2
1 Solution
 
ExcelRayCommented:
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
 
thomasdoddsCommented:
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
 
thomasdoddsCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
thomasdoddsCommented:
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
 
thomasdoddsCommented:
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
 
HWilfongAuthor Commented:
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
 
thomasdoddsCommented:
<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
 
thomasdoddsCommented:
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
 
HWilfongAuthor Commented:
Thank you very much thomasdodds.
Excellent!
0
 
thomasdoddsCommented:
You are very welcome!

I have found the following to be of great help:

http://www.devguru.com
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now