Solved

# ASP.NET - VB.NET - How to do difference calculation between two date fields on a form and split the result between two other form textboxes (years and months)?

Posted on 2006-04-14
353 Views
I have an aspx form where data entry is being done.  I have these textbox fields on the form:
Date Collected:  textbox
Date of Onset:  textbox

I would like to be able to do a calculation, based on the dates entered in those two fields.  I want to know the difference between date collected (later date) and date of onset (earlier date) and display the difference measured in how many years and months the difference is in two additional textboxes on the form (TimeSinceOnsetYrs, TimeSinceOnsetMonths).

Ex:
Date Collected:  1/1/2006
Date of Onset:  12/1/2003
Time Since Onset:  2 years, 1 month

I want it rounded to the nearest whole month.

Can someone help me with how to implement this??
0
Question by:sah18

LVL 35

Expert Comment

iMonths = DateDiff(DateInterval.Month, txtOnset.Text, txtCollected.Text)
iYears = Math.Floor(iMonths / 12)
iMonths=iMonths Mod 12
0

LVL 35

Expert Comment

Dim iYears, iMonths As Integer
iMonths = DateDiff(DateInterval.Month, CDate(txtOnset.Text), CDate(txtCollected.Text))
iYears = Math.Floor(iMonths / 12)
iMonths = iMonths Mod 12
TimeSinceOnsetYrs.Text = iYears
TimeSinceOnsetMonths.Text = iMonths
0

LVL 9

Author Comment

YZlat - Thanks very much for your help!

What event should I place this in?  Also, I want to make sure that the calculation doesn't error if either or both of the date textboxes haven't been filled in yet.  Do you know how to check for that?
0

LVL 35

Accepted Solution

you can place this code in either Page_Load event

If IsPostBack Then
...

End if

or in Button Click event

Dim iYears, iMonths As Integer
Try
If Trim(txtOnset.Text)<> "" AND Trim(txtCollected.Text)<>"" Then
iMonths = DateDiff(DateInterval.Month, CDate(txtOnset.Text), CDate(txtCollected.Text))
iYears = Math.Floor(iMonths / 12)
iMonths = iMonths Mod 12
TimeSinceOnsetYrs.Text = iYears
TimeSinceOnsetMonths.Text = iMonths
End if
Catch ex as Exception
Console.Write(ex.Message)
End Try
0

LVL 9

Author Comment

thank you!
0

## Featured Post

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
how to add IIS SMTP to handle application/Scanner relays into office 365.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…