Database 在Microsoft Access中计算年龄(以年和月为单位)(2010年)
我有两个字段:体检日期和出生日期。我以年为单位计算了年龄体检日期-出生日期/365.25。我想做的是在单独的字段中以年和月为单位计算年龄。我不确定是否可以使用code builder或其他方法来完成此操作。与年度计算一样,需要更多的时间来处理更精细的细节:Database 在Microsoft Access中计算年龄(以年和月为单位)(2010年),database,ms-access,vba,ms-access-2010,Database,Ms Access,Vba,Ms Access 2010,我有两个字段:体检日期和出生日期。我以年为单位计算了年龄体检日期-出生日期/365.25。我想做的是在单独的字段中以年和月为单位计算年龄。我不确定是否可以使用code builder或其他方法来完成此操作。与年度计算一样,需要更多的时间来处理更精细的细节: Public Function Months( _ ByVal datDate1 As Date, _ ByVal datDate2 As Date, _ Optional ByVal booLinear As Boolean)
Public Function Months( _
ByVal datDate1 As Date, _
ByVal datDate2 As Date, _
Optional ByVal booLinear As Boolean) _
As Integer
' Returns the difference in full months between datDate1 and datDate2.
'
' Calculates correctly for:
' negative differences
' leap years
' dates of 29. February
' date/time values with embedded time values
' negative date/time values (prior to 1899-12-29)
'
' Optionally returns negative counts rounded down to provide a
' linear sequence of month counts.
' For a given datDate1, if datDate2 is decreased stepwise one month from
' returning a positive count to returning a negative count, one or two
' occurrences of count zero will be returned.
' If booLinear is False, the sequence will be:
' 3, 2, 1, 0, 0, -1, -2
' If booLinear is True, the sequence will be:
' 3, 2, 1, 0, -1, -2, -3
'
' If booLinear is False, reversing datDate1 and datDate2 will return
' results of same absolute Value, only the sign will change.
' This behaviour mimics that of Fix().
' If booLinear is True, reversing datDate1 and datDate2 will return
' results where the negative count is offset by -1.
' This behaviour mimics that of Int().
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of months to dates of Feb. 29.
' when the resulting year is a common year.
'
' 2010-03-30. Cactus Data ApS, CPH.
Dim intDiff As Integer
Dim intSign As Integer
Dim intMonths As Integer
' Find difference in calendar months.
intMonths = DateDiff("m", datDate1, datDate2)
' For positive resp. negative intervals, check if the second date
' falls before, on, or after the crossing date for a 1 month period
' while at the same time correcting for February 29. of leap years.
If DateDiff("d", datDate1, datDate2) > 0 Then
intSign = Sgn(DateDiff("d", DateAdd("m", intMonths, datDate1), datDate2))
intDiff = Abs(intSign < 0)
Else
intSign = Sgn(DateDiff("d", DateAdd("m", -intMonths, datDate2), datDate1))
If intSign <> 0 Then
' Offset negative count of months to continuous sequence if requested.
intDiff = Abs(booLinear)
End If
intDiff = intDiff - Abs(intSign < 0)
End If
' Return count of months as count of full 1 month periods.
Months = intMonths - intDiff
End Function
与年度计算一样,需要更多的时间来处理更精细的细节:
Public Function Months( _
ByVal datDate1 As Date, _
ByVal datDate2 As Date, _
Optional ByVal booLinear As Boolean) _
As Integer
' Returns the difference in full months between datDate1 and datDate2.
'
' Calculates correctly for:
' negative differences
' leap years
' dates of 29. February
' date/time values with embedded time values
' negative date/time values (prior to 1899-12-29)
'
' Optionally returns negative counts rounded down to provide a
' linear sequence of month counts.
' For a given datDate1, if datDate2 is decreased stepwise one month from
' returning a positive count to returning a negative count, one or two
' occurrences of count zero will be returned.
' If booLinear is False, the sequence will be:
' 3, 2, 1, 0, 0, -1, -2
' If booLinear is True, the sequence will be:
' 3, 2, 1, 0, -1, -2, -3
'
' If booLinear is False, reversing datDate1 and datDate2 will return
' results of same absolute Value, only the sign will change.
' This behaviour mimics that of Fix().
' If booLinear is True, reversing datDate1 and datDate2 will return
' results where the negative count is offset by -1.
' This behaviour mimics that of Int().
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of months to dates of Feb. 29.
' when the resulting year is a common year.
'
' 2010-03-30. Cactus Data ApS, CPH.
Dim intDiff As Integer
Dim intSign As Integer
Dim intMonths As Integer
' Find difference in calendar months.
intMonths = DateDiff("m", datDate1, datDate2)
' For positive resp. negative intervals, check if the second date
' falls before, on, or after the crossing date for a 1 month period
' while at the same time correcting for February 29. of leap years.
If DateDiff("d", datDate1, datDate2) > 0 Then
intSign = Sgn(DateDiff("d", DateAdd("m", intMonths, datDate1), datDate2))
intDiff = Abs(intSign < 0)
Else
intSign = Sgn(DateDiff("d", DateAdd("m", -intMonths, datDate2), datDate1))
If intSign <> 0 Then
' Offset negative count of months to continuous sequence if requested.
intDiff = Abs(booLinear)
End If
intDiff = intDiff - Abs(intSign < 0)
End If
' Return count of months as count of full 1 month periods.
Months = intMonths - intDiff
End Function
虽然DateDiff函数似乎是计算年龄的逻辑选择,但不幸的是,它没有计算两个日期之间经过的完整年份或月份数。例如,假设一个婴儿于2014年12月31日出生,并在48小时后的2015年1月2日接受了检查。就是
DateOfBirth=DateSerial2014,12,31
DateOfExam=DateSerial2015,1,2
如果我们简单地用DateDiff来计算她考试时的年龄,我们会得到
?DateDiffyyyy,DateOfBirth,DateOfExam
1.
?DateDiffm,DateOfBirth,DateOfExam
1.
所以,我们会报告婴儿是1岁1个月大,而实际上她只有2天大
正确的年龄计算需要比这更复杂。以下VBA函数将以年和月为单位计算年龄,返回类似于2年和1个月的字符串:
公共函数ageInYears和Months开始日期作为变量,结束日期作为变量作为变量
Dim Date1作为日期,Date2作为日期
Dim mm1为整数,dd1为整数,mm2为整数,dd2为整数
Dim AGE YERS为整数,AGEMONTS为整数,rtn为变量
rtn=Null
如果不是IsNullStartDate或IsNullEndDate,则
如果起始日期mm2或mm1=mm2且dd1>dd2,则
年龄年=年龄年-1
如果结束
AGEMONTS=DateDiffm,Date1,Date2 Mod 12
如果dd1>dd2,则
如果AGEMONTS=0,则
月数=12
如果结束
AGEMONTS=AGEMONTS-1
如果结束
如果ageYears=0,ageMonths=0,则
rtn=少于1个月
其他的
rtn=年龄年、年、月、月、月、年、月=1,秒
如果结束
如果结束
年龄年和月=rtn
端函数
虽然DateDiff函数似乎是计算年龄的逻辑选择,但不幸的是,它没有计算两个日期之间经过的完整年份或月份数。例如,假设一个婴儿于2014年12月31日出生,并在48小时后的2015年1月2日接受了检查。就是
DateOfBirth=DateSerial2014,12,31
DateOfExam=DateSerial2015,1,2
如果我们简单地用DateDiff来计算她考试时的年龄,我们会得到
?DateDiffyyyy,DateOfBirth,DateOfExam
1.
?DateDiffm,DateOfBirth,DateOfExam
1.
所以,我们会报告婴儿是1岁1个月大,而实际上她只有2天大
正确的年龄计算需要比这更复杂。以下VBA函数将以年和月为单位计算年龄,返回类似于2年和1个月的字符串:
公共函数ageInYears和Months开始日期作为变量,结束日期作为变量作为变量
Dim Date1作为日期,Date2作为日期
Dim mm1为整数,dd1为整数,mm2为整数,dd2为整数
Dim AGE YERS为整数,AGEMONTS为整数,rtn为变量
rtn=Null
如果不是IsNullStartDate或IsNullEndDate,则
如果起始日期mm2或mm1=mm2且dd1>dd2,则
年龄年=年龄年-1
如果结束
AGEMONTS=DateDiffm,Date1,Date2 Mod 12
如果dd1>dd2,则
如果AGEMONTS=0,则
月数=12
如果结束
AGEMONTS=AGEMONTS-1
如果结束
如果ageYears=0,ageMonths=0,则
rtn=少于1个月
其他的
rtn=年龄年、年、月、月、月、年、月=1,秒
如果结束
如果结束
年龄年和月=rtn
端函数
检查DateDiff@Fionnula,我看到必须在DateDiff中指定间隔。因此,年龄可能是以年、月或其他一些为单位。有没有可能在不同的领域有年复一年,比如说10年零5个月,年复一月?@Fionnula,我很抱歉,我以前没有仔细看过这些例子。它以年、月、日为单位计算。谢谢。看看DateDiff@Fionnula,我看到间隔必须在DateDiff中指定。因此,年龄可能是以年、月或其他一些为单位。有没有可能在不同的领域有年复一年,比如说10年零5个月,年复一月?@Fionnula,我很抱歉,我以前没有仔细看过这些例子。它以年、月、日为单位计算。谢谢。你错过的是考虑闰年。这是困难的部分,但DateAdd会处理的
当我用2012/02/29测试上述代码时,它的工作方式与我预期的一样。对于非闰年,在02/29生日的人在02/28仍然是n岁,在03/01仍然是n+1岁。我错过了什么?你错过了跳跃-那些出生在2月29日-例如,与DOB 2012-02-29,他/她将在2015年2月28日的最后一天3岁0个月。你可以找到一些参考资料,声称它将首先发生在3月1日。如果人们这么认为,没问题,但大多数机构和组织都遵循这样一条规则:你生日的月份应该与你生日的月份相匹配。你错过的是把闰年考虑在内。这是困难的一部分,但是DateAdd会处理它。当我用2012/02/29测试上面的代码时,它像我预期的那样工作。对于非闰年,在02/29生日的人在02/28仍然是n岁,在03/01仍然是n+1岁。我错过了什么?你错过了跳跃-那些出生在2月29日-例如,与DOB 2012-02-29,他/她将在2015年2月28日的最后一天3岁0个月。你可以找到一些参考资料,声称它将首先发生在3月1日。如果人们这么认为,没问题,但大多数机构和组织都遵循这样的规则:你生日的月份应该与你生日的月份相匹配。