Click here to Skip to main content
15,906,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there

I want to calculate the age of each member in the member table.First I will select all the date of births from the database using a sql statement but I want to store them in something like a date time object so I can be able to calculate the age using date time.Now.How can I do this?

PS: Sample code will be a great help!!!!

Thanx
Posted
Updated 21-Sep-11 5:00am
v2

Well, I would start by storing them as DateTime in the database. If you don't, and you are storing a string, then you are leaving yourself wide open to problems due to the unknown-at-time-of-reading date format of the user entering the data in the first place. If they are American, dates will be MM/DD/YYYY, European DD/MM/YYYY, Japanese YYYY/MM/DD. If you don't know which format they are in as strings, then you have a major job (and probably lots of errors) converting them to a useful date format.

Once you have them in DateTime in the database, you can just cast them to a .NET DateTime and off you go.
 
Share this answer
 
Load the "date of birth" object in a List or Collection object of an entity which has at least two properties 1) DateOfBirth and 2)Age. Once the Dob is loaded in the collection you can iterate through them and calculate age for each element and store them in the Age property. Now you are all set to display the Age on Ui or store in Db.

Hope it's helpful...
 
Share this answer
 
Hi
you can have sql statement to retrieve member name and age

SQL
Select Name, DATEDIFF(MM,dob, GETDATE()) From Member


or

If you wanted to perform through programming

read MemberName and DOB to DataTable and
C#
DataTable tab = new DataTable();
          var stateList = from row in tab1.AsEnumerable().AsQueryable()
                          select new { Name = row["Name"], Age = DateTime.Now.Subtract(DateTime.Parse(row["DOB"].ToString())).TotalDays / 30 };
 
Share this answer
 
You can use the Time Period Library for .NET[^] to calculate the age:
static void PrintAge( DbConnection connection )
{
  string queryString = "SELECT DateOfBirth FROM Users";

  using (connection)
  {
    try
    {
      DbCommand command = connection.CreateCommand();
      command.CommandText = queryString;
      command.CommandType = CommandType.Text;
      connection.Open();

      DbDataReader reader = command.ExecuteReader();
      DateTime now = DateTime.Now;
      while ( reader.Read() )
      {
        object dateOfBirth = reader[0];
        if ( dateOfBirth == null || dateOfBirth.GetType() != typeof( DateTime ) )
        {
          continue;
        }
        Console.WriteLine("Age: {0}", DateDiff( (DateTime)dateOfBirth ).ElapsedYears );
      }
    }
    catch ( Exception ex )
    {
      Console.WriteLine( "Exception.Message: {0}", ex.Message );
    }
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900