Date Difference / Years and Months Elapsed

mmunoz

New member
Joined
Aug 23, 2008
Messages
3
Programming Experience
Beginner
Can anyone guide me as to how to display on a text box the number of years and months that have past between today and the date from a calendar datepicker placed on a form from which the user will select the account opening date. I want display on the text box "This account is x years and x months old".

Here is the code that I am using, but of course is incomplete. Please correct it accordingly. Thanks.

VB.NET:
 Dim Inception As Date = DateTimePicker1.Value
        Dim Today As Date = DateTime.Today
        Dim Span As TimeSpan = Today.Subtract(DateTimePicker1.Value)

        TextBox2.Text = Span.TotalDays.ToString()


Thanks so much!

Manuel
 
Your big problem is that timespan doesnt run into months or years. Just abuse a date instead:


VB.NET:
Dim ts as TimeSpan = DateTime.Now - DateTimePicker1.Value

Dim dt as DateTime = DateTime.MinValue + ts

Dim msg as String = String.Format("This account is (over) {0} years and {1} months old", dt.Year-1, dt.Month-1)
 
Back
Top