How to display date and calculate age

jpdbay

Active member
Joined
Feb 22, 2006
Messages
31
Programming Experience
Beginner
Hi,

I'm capturing date of birth in DateTimePicker ("MM/DD/YYYY" format).
1. How do I display the date into "MM/DD/YYYY"?

2. How do I calculate age from the DateTimePicker using "MM/DD/YYYY" format?

Anyone, with ideas or code of sample will be much appreciated. Thanx

Rgds,
jpdbay
 
Last edited by a moderator:
Hey,

Please see my Inline Comments:

I'm capturing date of birth in DateTimePicker ("MM/DD/YYYY" format).
1. How do I display the date into "MM/DD/YYYY"?

Jay- a. Add a DatetimePicker control on your form.
b. Go to the property Window.
c. In CustomFormat Property add "MM/dd/yyy"
d. In Format Choose Custom Option.
Your DateTimePicker will Show DateTime value as "MM/dd/yyy"
2. How do I calculate age from the DateTimePicker using "MM/DD/YYYY" format?

Jay - I have copied a very simple code just to show you how to calculate .
But u can still Implement a better way
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dtNow [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Date
[/COLOR][/SIZE][SIZE=2]dtNow = DateTime.Now.Date [/SIZE][SIZE=2][COLOR=#008000]' Current Date
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Age [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Number of days.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dob [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Date[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Date Of Birth Entered By User
[/COLOR][/SIZE][SIZE=2]dob = DateTimePicker1.Value.Date [/SIZE][SIZE=2][COLOR=#008000]' Collect Date of Birth
[/COLOR][/SIZE][SIZE=2]Age = dob.Subtract(dtNow).Days [/SIZE][SIZE=2][COLOR=#008000]' Get the Number of days.
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Do u r own calculations how want to Represent Age
[/COLOR][/SIZE][code]
 
- Jay
 
hi Jay,
color.gif


Thanx 4ur comment n comment. Anyway, the formula for Age in the code should be:
Age = dtNow.Subtract(dob).Days ' Get the Number of days.
else it rtns -ve value.

Thanx 4ur guidance.

rgds,
jpdbay

I
 
Back
Top