从数据库计算年龄和出生日期 [英] Calculate age with date of birth from database

查看:143
本文介绍了从数据库计算年龄和出生日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想计算成员表中每个成员的年龄.首先,我将使用sql语句从数据库中选择所有出生日期,但我想将它们存储在日期时间对象中,以便能够计算日期时间的年龄.现在.该怎么办?

PS:示例代码将为您提供很大的帮助!
Thanx

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

推荐答案

好吧,我首先将它们作为DateTime存储在数据库中.如果您不这样做,并且您正在存储一个字符串,那么由于首先输入数据的用户的读取时间未知日期格式,您将面临各种问题.如果是美国,则日期将是MM/DD/YYYY,欧洲DD/MM/YYYY,日本YYYY/MM/DD.如果您不知道它们以字符串形式使用哪种格式,那么您将需要进行大量工作(并且可能会出现很多错误),将它们转换为有用的日期格式.

一旦将它们放在数据库的DateTime中,就可以将它们强制转换为.NET DateTime,然后就可以使用.
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.


将出生日期"对象加载到List或Collection对象中具有至少两个属性的实体的名称1)DateOfBirth和2)Age.将Dob加载到集合中之后,您可以遍历它们并计算每个元素的年龄并将其存储在Age属性中.现在,您都可以在Ui上显示年龄"或在Db中存储.

希望对您有帮助...
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...



您可以使用sql语句来检索成员名称和年龄

Hi
you can have sql statement to retrieve member name and age

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





如果您想通过编程来执行

将MemberName和DOB读取到DataTable和



or

If you wanted to perform through programming

read MemberName and DOB to DataTable and

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 };


这篇关于从数据库计算年龄和出生日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆