Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Calculate age using C#

An efficient C# method for calculating the age in years by passing in the birth date.

On Saturday, February 9th 2008 at 08:41 PM
By Andrew Pociu (View Profile)
***--   (Rated 3 with 7 votes)
Contextual Ads
More C# Resources
Advertisement
  1. public static int CalculateAge(DateTime BirthDate)
  2. {
  3.     int YearsPassed = DateTime.Now.Year - BirthDate.Year;
  4.     // Are we before the birth date this year? If so subtract one year from the mix
  5.     if (DateTime.Now.Month < BirthDate.Month || (DateTime.Now.Month == BirthDate.Month && DateTime.Now.Day < BirthDate.Day))
  6.     {
  7.         YearsPassed--;
  8.     }
  9.     return YearsPassed;
  10. }
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
by Bhaskar on Tuesday, July 28th 2009 at 12:35 AM

Thank you.

by shamu on Monday, December 28th 2009 at 05:37 AM

Finally i came up with a own code, i created separate class for Age Calculation, below Program gives exact age, eg: 24 Years 5 Months 2 days.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BabyImageNameChanger
{
class AgeCalculator
{
#region PrivateMembers
private int _Years, _Months, _Days;
private bool _IsBirthday;
private DateTime _startDate;
private DateTime _endDate;
private TimeSpan _daysDiff;
#endregion

#region Read Only Property
public int Years
{
get
{
return _Years;
}
}
public int Months
{
get
{
return _Months;
}
}
public int Days
{
get
{
return _Days;
}
}
public bool IsBirthday
{
get
{
return _IsBirthday;
}
}
public string AgeInString
{
get
{
StringBuilder result = new StringBuilder();
if (IsBirthday)
{
if (Years != 0)
{
result.Append(Years.ToString());
result.Append(" ");
}

result.Append("Birthday");
}
else
{
if (Years != 0)
{
result.Append(Years.ToString());
result.Append(" Year");
if (Years > 1) result.Append("s");
result.Append(" ");

}

if (Months != 0)
{
result.Append(Months.ToString());
result.Append(" Month");
if (Months > 1) result.Append("s");
result.Append(" ");
}

if (Days != 0)
{
result.Append(Days.ToString());
result.Append(" Day");
if (Days > 1) result.Append("s");
result.Append(" ");
}

}

return result.ToString().Trim();
}
}
#endregion

#region Public Methods
public AgeCalculator()
{

}
///
/// Constructor for the Age Calculation
///
/// Date of Birth
/// Comparsion Date
public AgeCalculator(DateTime dateOfBirth, DateTime compareDate)
{
CalculateAge(dateOfBirth, compareDate);
}
///
/// Calculates the age, and returns the age in string format
///
///
///
///
public string GetAgeInString(DateTime dateOfBirth, DateTime compareDate)
{
CalculateAge(dateOfBirth, compareDate);
return AgeInString;
}
///
/// Calculates the Age
///
/// Date of Birth
/// Comparsion Date
public void CalculateAge(DateTime dateOfBirth, DateTime compareDate)
{
Initialize();

if (dateOfBirth < compareDate)
{
_startDate = dateOfBirth;
_endDate = compareDate;
}
else
{
_startDate = compareDate;
_endDate = dateOfBirth;
}


CalculateYears();

if ((_startDate.Day == _endDate.Day)

by shamu on Monday, December 28th 2009 at 05:37 AM

Finally i came up with a own code, i created separate class for Age Calculation, below Program gives exact age, eg: 24 Years 5 Months 2 days.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BabyImageNameChanger
{
class AgeCalculator
{
#region PrivateMembers
private int _Years, _Months, _Days;
private bool _IsBirthday;
private DateTime _startDate;
private DateTime _endDate;
private TimeSpan _daysDiff;
#endregion

#region Read Only Property
public int Years
{
get
{
return _Years;
}
}
public int Months
{
get
{
return _Months;
}
}
public int Days
{
get
{
return _Days;
}
}
public bool IsBirthday
{
get
{
return _IsBirthday;
}
}
public string AgeInString
{
get
{
StringBuilder result = new StringBuilder();
if (IsBirthday)
{
if (Years != 0)
{
result.Append(Years.ToString());
result.Append(" ");
}

result.Append("Birthday");
}
else
{
if (Years != 0)
{
result.Append(Years.ToString());
result.Append(" Year");
if (Years > 1) result.Append("s");
result.Append(" ");

}

if (Months != 0)
{
result.Append(Months.ToString());
result.Append(" Month");
if (Months > 1) result.Append("s");
result.Append(" ");
}

if (Days != 0)
{
result.Append(Days.ToString());
result.Append(" Day");
if (Days > 1) result.Append("s");
result.Append(" ");
}

}

return result.ToString().Trim();
}
}
#endregion

#region Public Methods
public AgeCalculator()
{

}
///
/// Constructor for the Age Calculation
///
/// Date of Birth
/// Comparsion Date
public AgeCalculator(DateTime dateOfBirth, DateTime compareDate)
{
CalculateAge(dateOfBirth, compareDate);
}
///
/// Calculates the age, and returns the age in string format
///
///
///
///
public string GetAgeInString(DateTime dateOfBirth, DateTime compareDate)
{
CalculateAge(dateOfBirth, compareDate);
return AgeInString;
}
///
/// Calculates the Age
///
/// Date of Birth
/// Comparsion Date
public void CalculateAge(DateTime dateOfBirth, DateTime compareDate)
{
Initialize();

if (dateOfBirth < compareDate)
{
_startDate = dateOfBirth;
_endDate = compareDate;
}
else
{
_startDate = compareDate;
_endDate = dateOfBirth;
}


CalculateYears();

if ((_startDate.Day == _endDate.Day)

by shamu on Monday, December 28th 2009 at 05:37 AM

Finally i came up with a own code, i created separate class for Age Calculation, below Program gives exact age, eg: 24 Years 5 Months 2 days.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BabyImageNameChanger
{
class AgeCalculator
{
#region PrivateMembers
private int _Years, _Months, _Days;
private bool _IsBirthday;
private DateTime _startDate;
private DateTime _endDate;
private TimeSpan _daysDiff;
#endregion

#region Read Only Property
public int Years
{
get
{
return _Years;
}
}
public int Months
{
get
{
return _Months;
}
}
public int Days
{
get
{
return _Days;
}
}
public bool IsBirthday
{
get
{
return _IsBirthday;
}
}
public string AgeInString
{
get
{
StringBuilder result = new StringBuilder();
if (IsBirthday)
{
if (Years != 0)
{
result.Append(Years.ToString());
result.Append(" ");
}

result.Append("Birthday");
}
else
{
if (Years != 0)
{
result.Append(Years.ToString());
result.Append(" Year");
if (Years > 1) result.Append("s");
result.Append(" ");

}

if (Months != 0)
{
result.Append(Months.ToString());
result.Append(" Month");
if (Months > 1) result.Append("s");
result.Append(" ");
}

if (Days != 0)
{
result.Append(Days.ToString());
result.Append(" Day");
if (Days > 1) result.Append("s");
result.Append(" ");
}

}

return result.ToString().Trim();
}
}
#endregion

#region Public Methods
public AgeCalculator()
{

}
///
/// Constructor for the Age Calculation
///
/// Date of Birth
/// Comparsion Date
public AgeCalculator(DateTime dateOfBirth, DateTime compareDate)
{
CalculateAge(dateOfBirth, compareDate);
}
///
/// Calculates the age, and returns the age in string format
///
///
///
///
public string GetAgeInString(DateTime dateOfBirth, DateTime compareDate)
{
CalculateAge(dateOfBirth, compareDate);
return AgeInString;
}
///
/// Calculates the Age
///
/// Date of Birth
/// Comparsion Date
public void CalculateAge(DateTime dateOfBirth, DateTime compareDate)
{
Initialize();

if (dateOfBirth < compareDate)
{
_startDate = dateOfBirth;
_endDate = compareDate;
}
else
{
_startDate = compareDate;
_endDate = dateOfBirth;
}


CalculateYears();

if ((_startDate.Day == _endDate.Day)

by alfredo on Wednesday, May 12th 2010 at 11:38 AM

(DateTime.Now - c.DateOfBirth).Days / 365);

by Mohsen on Tuesday, November 23rd 2010 at 05:05 AM

Calculate Age

by Phen375 on Monday, December 19th 2011 at 06:00 PM

Your personal know-how and kindness in maneuvering every part was tremendous. I am not sure what I would've done if I hadn't encountered such a solution like this. I'm able to at this time look ahead to my future. Thanks a lot so much for the specialized and result oriented help. I won't hesitate to endorse the blog to anybody who desires assistance on this topic.

by GenF20 Plus on Monday, December 19th 2011 at 06:02 PM

Your personal know-how and kindness in maneuvering every part was tremendous. I am not sure what I would've done if I hadn't encountered such a solution like this. I'm able to at this time look ahead to my future. Thanks a lot so much for the specialized and result oriented help. I won't hesitate to endorse the blog to anybody who desires assistance on this topic.

by GenFX on Monday, December 19th 2011 at 06:03 PM

Your personal know-how and kindness in maneuvering every part was tremendous. I am not sure what I would've done if I hadn't encountered such a solution like this. I'm able to at this time look ahead to my future. Thanks a lot so much for the specialized and result oriented help. I won't hesitate to endorse the blog to anybody who desires assistance on this topic.

by Teeth Whitening Reviews on Tuesday, December 20th 2011 at 01:39 AM

I truly wanted to write a quick message to thank you for some of the splendid points you are showing at this website. My long internet investigation has at the end been compensated with sensible facts and techniques to share with my family and friends.

by Human Growth Hormone on Tuesday, December 20th 2011 at 01:41 AM

I truly wanted to write a quick message to thank you for some of the splendid points you are showing at this website. My long internet investigation has at the end been compensated with sensible facts and techniques to share with my family and friends.

by Best Diet Pills on Tuesday, December 20th 2011 at 01:45 AM

I would believe that we visitors are truly blessed to dwell in a wonderful network with many wonderful professionals with very beneficial hints. I feel extremely privileged to have seen the web pages and look forward to many more pleasurable times reading here. Thank you once again for everything.

by HGH Advanced on Tuesday, December 20th 2011 at 01:54 AM

I would believe that we visitors are truly blessed to dwell in a wonderful network with many wonderful professionals with very beneficial hints. I feel extremely privileged to have seen the web pages and look forward to many more pleasurable times reading here. Thank you once again for everything.

by Dental White on Tuesday, December 20th 2011 at 01:57 AM


Thanks so much for providing individuals with an extremely remarkable possiblity to read critical reviews from this web site. It is always so beneficial plus jam-packed with a great time for me personally and my office friends to search the blog at the least three times in one week to see the fresh secrets you will have.

by Pure Acai Berry Max on Tuesday, December 20th 2011 at 01:58 AM


Thanks so much for providing individuals with an extremely remarkable possiblity to read critical reviews from this web site. It is always so beneficial plus jam-packed with a great time for me personally and my office friends to search the blog at the least three times in one week to see the fresh secrets you will have.

by Ultimate Maqui Berry on Tuesday, December 20th 2011 at 02:01 AM

I am just always happy with your good tips and hints served by you. Some 1 areas in this article are undeniably the finest I've had.

by Proactol Plus on Tuesday, December 20th 2011 at 02:05 AM

I am just always happy with your good tips and hints served by you. Some 1 areas in this article are undeniably the finest I've had.

by Provacyl on Tuesday, December 20th 2011 at 02:10 AM

I am just always happy with your good tips and hints served by you. Some 1 areas in this article are undeniably the finest I've had.

by Nuratrim on Tuesday, December 20th 2011 at 02:14 AM

Thanks a lot for providing individuals with remarkably special possiblity to read critical reviews from this web site. It can be very kind plus packed with a great time for me personally and my office mates to visit your site no less than three times weekly to study the newest tips you have.

by Capsiplex on Tuesday, December 20th 2011 at 02:20 AM

Thanks a lot for providing individuals with remarkably special possiblity to read critical reviews from this web site. It can be very kind plus packed with a great time for me personally and my office mates to visit your site no less than three times weekly to study the newest tips you have.

by Michal on Tuesday, January 24th 2012 at 06:24 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by Hussain Md. Fahad on Monday, February 6th 2012 at 05:24 AM

Thanks

by manchester signs on Thursday, April 19th 2012 at 03:56 AM

r Age Calculation, below Program gives exact age, eg: 24 Years 5 Months 2 days.

by rfg on Saturday, August 25th 2012 at 06:59 AM

rtredf

by Austin sclerotherapy on Saturday, October 13th 2012 at 06:13 AM

Thanks so much for providing individuals with an extremely remarkable possiblity to read critical reviews from this web site. It is always so beneficial plus jam-packed with a great time for me personally and my office friends to search the blog at the least three times in one week to see the fresh secrets you will have.

by powdered milk substitute on Thursday, November 1st 2012 at 05:56 AM

office friends to search the blog at the least three times in one week to see the fresh secrets you will have.


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs C# Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources