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

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??
LVL 9
sah18Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

YZlatCommented:
iMonths = DateDiff(DateInterval.Month, txtOnset.Text, txtCollected.Text)
iYears = Math.Floor(iMonths / 12)
iMonths=iMonths Mod 12
0
YZlatCommented:
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
sah18Author Commented:
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
YZlatCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sah18Author Commented:
thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.