从数据库计算年龄和出生日期

编程入门 行业动态 更新时间:2024-10-26 22:18:04
本文介绍了从数据库计算年龄和出生日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好, 我想计算成员表中每个成员的年龄.首先,我将使用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 };

更多推荐

从数据库计算年龄和出生日期

本文发布于:2023-10-18 11:50:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504154.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:出生日期   年龄   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!