How do you do date subtraction?

emaduddeen

Well-known member
Joined
May 5, 2010
Messages
171
Location
Lowell, MA & Occasionally Indonesia
Programming Experience
Beginner
Hi Everyone,

In my "Select" statement I have this:

Int((Date()-dob)/365.25) AS Age

I would like to do the same thing in VB .NET code. Can you show me how to do it?

I plan to do something like:

editBoxAge.Text = .......

dob would be stored in datePickerDob.Text

Thanks.

Truly,
Emad
 
You can use the DateTime.Today minus datePickerDob.Value to get a TimeSpan and from there it's a matter of calculating the years
 
More like this:
VB.NET:
Dim ts As TimeSpan = DateTime.Today.Subtract(DateTimePicker1.Value)
Me.Text = ts.Days.ToString
 
Hi,

Thanks for the help. :D

I used this based on your code:

VB.NET:
        ' Calculate age.
        '---------------
        Dim ts As TimeSpan = DateTime.Today.Subtract(DatePickerDob.Value)

        EditBoxAge.Text = Int(ts.Days.ToString / 365)

I'm not sure why it works so I need to look at peter's blog and find out about TimeSpan.

Truly,
Emad
 
Back
Top