计算年龄
时 间: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交流群 (群号:483923997) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- 【Access小作品】简单的待...(04.30)
- 【Access窗体导出PDF】...(04.08)
- 【Access窗体导出PDF】...(04.07)
- Access两种方式实现即时更...(03.01)
- Access隐藏与显示lacc...(01.12)
- 【Access高效办公】将每个...(12.23)
- Access21点游戏源代码(12.13)
- 【Access窗体导出Exce...(11.15)
- 【Access开发】Acces...(11.14)
学习心得
最新文章
- 【Access小作品】简单的待办任...(04.30)
- 从另一个ACCESS数据库批量导入...(04.29)
- Access日期格式的数据导出Ex...(04.28)
- ACCESS精华集锦资料.CHM(04.25)
- Access VBA语句If Me...(04.24)
- 【Access修改记录示例】编辑选...(04.22)
- 【Access表名称命名建议】将A...(04.20)
- Access学习笔记--用Acce...(04.19)
- 【Access重复项查询示例】将A...(04.17)
- Access快速开发平台企业版--...(04.16)