Home > ASP.Net > Get Age from BirthDate in Days and Months and Years

Get Age from BirthDate in Days and Months and Years


Hi all,

try this example to Calculate age from BirthDate in Days and Months and Years

We will use this C# Method which take BirthDate as Parameter and Returns String of Number of Days and Months and Years

private string GetAge(DateTime birthday)
    {
        DateTime Now = DateTime.Now;
        int Years = new DateTime(DateTime.Now.Subtract(birthday).Ticks).Year - 1;
        DateTime dtPastYearDate = birthday.AddYears(Years);
        int Months = 0;
        for (int i = 1; i <= 12; i++)
        {
            if (dtPastYearDate.AddMonths(i) == Now)
            {
                Months = i; break;
            }
            else if (dtPastYearDate.AddMonths(i) >= Now)
            {
                Months = i - 1; break;
            }
        }
        int Days = Now.Subtract(dtPastYearDate.AddMonths(Months)).Days;
        int Hours = Now.Subtract(dtPastYearDate).Hours;
        int Minutes = Now.Subtract(dtPastYearDate).Minutes;
        int Seconds = Now.Subtract(dtPastYearDate).Seconds;
        return String.Format("Age: {0} Year(s) {1} Month(s) {2} Day(s) {3} Hour(s) {4} Second(s)", Years, Months, Days, Hours, Seconds);
    }

and to use this method :-

GetAge(DateTime.Parse("01/21/1984"))

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: