计算年龄
时 间: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对子窗体数据进行批...(10.30)
- 最精简的组合框行来源数据快速输...(10.25)
- Access仿平台的多值选择器...(10.24)
- 【Access日期区间段查询】...(10.22)
- 【Access源码示例】VBA...(10.12)
- Access累乘示例,Acce...(10.09)
- 数值8.88,把整数8去掉,转...(10.08)
- 【Access自定义函数】一个...(09.30)
- 【Access选项卡示例】Ac...(09.09)
学习心得
最新文章
- Access快速开发平台企业版--...(11.18)
- 不会用多表联合查询,多表查询没结果...(11.16)
- 【案例分享】主键字段值含有不间断空...(11.16)
- Access快速开发平台--后台D...(11.14)
- 微软Access邀测新Monaco...(11.12)
- Access列表框左右互选、列表框...(11.11)
- 高效率在导入数据前删除记录(11.10)
- Access报价单转订单示例代码(11.08)
- Access系统自带的日期选择器不...(11.08)
- 分享一下Access工程中的acw...(11.07)