Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
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
5
Medium Priority
?
369 Views
Last Modified: 2012-06-27
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
Comment
Question by:sah18
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 16452993
iMonths = DateDiff(DateInterval.Month, txtOnset.Text, txtCollected.Text)
iYears = Math.Floor(iMonths / 12)
iMonths=iMonths Mod 12
0
 
LVL 35

Expert Comment

by:YZlat
ID: 16453024
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

by:sah18
ID: 16453103
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

by:
YZlat earned 1000 total points
ID: 16453866
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

by:sah18
ID: 16455907
thank you!
0

Featured Post

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.

Question has a verified solution.

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

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…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

580 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