计算年龄
时 间:2007-11-15 09:44:13
作 者:ksiv ID:11 城市:上海 QQ:3002789054
摘 要:有一个人员花名册,有"出生年月"字段(格式yyyy-mm),如何计算出每个人的年龄,和全部人员的平均年龄.
正 文:
(Q) How do I calculate the age of a person given his/her birthdate? (A) There are several methods to do this. I'll list two methods that have been posted to the newsgroups in the recent past: Assuming that the birthdate field is called [BDate] and is of type date, you can use the following calculation Age=DateDiff("yyyy", [Bdate], Now())+ _ Int( Format(now(), "mmdd") < Format( [Bdate], "mmdd") ) Alternate: You can use this function to calculate Age. Function Age(Bdate, DateToday) As Integer ' Returns the Age in years between 2 dates ' Doesn't handle negative date ranges i.e. Bdate > DateToday If Month(DateToday) < Month(Bdate) Or (Month(DateToday) = _ Month(Bdate) And Day(DateToday) < Day(Bdate)) Then Age = Year(DateToday) - Year(Bdate) - 1 Else Age = Year(DateToday) - Year(Bdate) End If End Function Here's another detailed Age Checker. '--- CODE START --- Public Sub CalcAge(vDate1 As Date, vdate2 As Date, ByRef vYears As Integer, ByRef vMonths As Integer, ByRef vDays As Integer) ' Comments : calculates the age in Years, Months and Days ' Parameters: ' vDate1 - D.O.B. ' vDate2 - Date to calculate age based on ' vYears - will hold the Years difference ' vMonths - will hold the Months difference ' vDays - will hold the Days difference vMonths = DateDiff("m", vDate1, vdate2) vDays = DateDiff("d", DateAdd("m", vMonths, vDate1), vdate2) If vDays < 0 Then ' wierd way that DateDiff works, fix it here vMonths = vMonths - 1 vDays = DateDiff("d", DateAdd("m", vMonths, vDate1), vdate2) End If vYears = vMonths \ 12 ' integer division vMonths = vMonths Mod 12 ' only want leftover less than one year End Sub '--- CODE END ---
Access软件网QQ交流群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- 【Access选项卡示例】Ac...(09.09)
- 【Access源码示例】按输入...(09.02)
- 【Access日期区间段查询】...(08.29)
- 【Access日期区间段查询】...(08.27)
- Access怎样才能实现日期时...(08.21)
- 【Access定时打开查询】A...(08.19)
- Access生成固定数量的记录...(08.13)
- Access怎样才能实现日期时...(08.12)
- Access利用导航窗体控件对...(08.03)
学习心得
最新文章
- Access表中的字段名、字段标题...(09.19)
- Access快速开发平台--更改“...(09.18)
- 【中秋及国庆优惠】Access培训...(09.15)
- Access如何将日期型的数值转换...(09.14)
- 英文输入法输入数据中存在单引号引起...(09.11)
- 【Access选项卡示例】Acce...(09.09)
- 让Access光标停留在指定的控件...(09.07)
- 关于Access查询条件里使用通配...(09.06)
- Access报表偷懒制作法--Ac...(09.05)
- Access快速开发平台--窗体数...(09.04)