Ms access 如何从基于给定日期的查询中找到一个人的年龄?
我如何通过查询从出生日期算起的给定日期来查找一个人的年龄 我想运行一个查询,返回表中每个人的年龄,从出生日期开始计算下一年的任何日期 我想以年为单位返回他们的年龄,比如说在明年的1/09(9月) 今天的日期是2015年9月2日 如果出生日期是2006年10月9日,答案是9 如果出生日期是2006年1月28日,答案是10Ms access 如何从基于给定日期的查询中找到一个人的年龄?,ms-access,vba,ms-access-2013,Ms Access,Vba,Ms Access 2013,我如何通过查询从出生日期算起的给定日期来查找一个人的年龄 我想运行一个查询,返回表中每个人的年龄,从出生日期开始计算下一年的任何日期 我想以年为单位返回他们的年龄,比如说在明年的1/09(9月) 今天的日期是2015年9月2日 如果出生日期是2006年10月9日,答案是9 如果出生日期是2006年1月28日,答案是10 有什么想法吗??有人吗?UNIX\u时间戳(NOW())-UNIX\u时间戳(表中的日期)以秒为单位显示差异。现在只需转换为年。您可以在表单上文本框的控制源中使用表达式 当前日期
有什么想法吗??有人吗?UNIX\u时间戳(NOW())-UNIX\u时间戳(表中的日期)以秒为单位显示差异。现在只需转换为年。您可以在表单上文本框的控制源中使用表达式 当前日期:2015年1月1日
Table1:
|Birth_Date |
|01/01/1995 | = 20
|01/01/1994 | = 21
表达方式:
=DateDiff(“yyyy”,[出生日期],日期())
每次打开表单时,年龄都会更新
或
VBA(需要一个按钮)
DateDiff仅返回日历年中的差异 对于今天的真实年龄,请使用以下函数:
Public Function AgeSimple( _
ByVal datDateOfBirth As Date) _
As Integer
' Returns the difference in full years from datDateOfBirth to current date.
'
' Calculates correctly for:
' leap years
' dates of 29. February
' date/time values with embedded time values
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of years to dates of Feb. 29.
' when the resulting year is a common year.
' After an idea of Markus G. Fischer.
'
' 2007-06-26. Cactus Data ApS, CPH.
Dim datToday As Date
Dim intAge As Integer
Dim intYears As Integer
datToday = Date
' Find difference in calendar years.
intYears = DateDiff("yyyy", datDateOfBirth, datToday)
If intYears > 0 Then
' Decrease by 1 if current date is earlier than birthday of current year
' using DateDiff to ignore a time portion of datDateOfBirth.
intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, datDateOfBirth)) > 0)
End If
AgeSimple = intAge
End Function
对于任何日期组合,请使用此函数:
Public Function Years( _
ByVal datDate1 As Date, _
ByVal datDate2 As Date, _
Optional ByVal booLinear As Boolean) _
As Integer
' Returns the difference in full years 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 year counts.
' For a given datDate1, if datDate2 is decreased step wise one year 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 years to dates of Feb. 29.
' when the resulting year is a common year.
'
' 2000-11-03. Cactus Data ApS, CPH.
' 2000-12-16. Leap year correction modified to be symmetrical.
' Calculation of intDaysDiff simplified.
' Renamed from YearsDiff() to Years().
' 2000-12-18. Introduced cbytMonthDaysMax.
' 2007-06-22. Version 2. Complete rewrite.
' Check for month end of February performed with DateAdd()
' after idea of Markus G. Fischer.
Dim intDiff As Integer
Dim intSign As Integer
Dim intYears As Integer
' Find difference in calendar years.
intYears = DateDiff("yyyy", datDate1, datDate2)
' For positive resp. negative intervals, check if the second date
' falls before, on, or after the crossing date for a full 12 months 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("yyyy", intYears, datDate1), datDate2))
intDiff = Abs(intSign < 0)
Else
intSign = Sgn(DateDiff("d", DateAdd("yyyy", -intYears, datDate2), datDate1))
If intSign <> 0 Then
' Offset negative count of years to continuous sequence if requested.
intDiff = Abs(booLinear)
End If
intDiff = intDiff - Abs(intSign < 0)
End If
' Return count of years as count of full 12 months periods.
Years = intYears - intDiff
End Function
公共职能年(_
ByVal DatDate 1作为日期_
ByVal DatDate 2作为日期_
可选的ByVal booLinear(作为布尔值)_
作为整数
'返回datDate1和datDate2之间的全年差值。
'
'正确计算:
"负差异",
“闰年
"29日。二月
'带有嵌入时间值的日期/时间值
'负日期/时间值(1899-12-29之前)
'
'可选地返回向下舍入的负计数,以提供
“年度计数的线性序列。
'对于给定的datDate1,如果datDate2从
'返回正计数到返回负计数,一个或两个
'将返回计数为零的事件。
'如果booLinear为False,则序列为:
' 3, 2, 1, 0, 0, -1, -2
'如果booLinear为真,则序列将为:
' 3, 2, 1, 0, -1, -2, -3
'
'如果booLinear为False,则将返回反向的datDate1和datDate2
'相同绝对值的结果,只有符号会改变。
'此行为模仿Fix()。
'如果booLinear为True,则将返回反向的datDate1和datDate2
'负计数被-1偏移的结果。
'此行为模仿Int()的行为。
'DateAdd()用于检查2月底,因为它正确
2月28日返回。在2月29日的日期上加上年数。
“当产生的年份是普通年份时。
'
' 2000-11-03. 仙人掌数据ApS,CPH。
' 2000-12-16. 闰年修正修改为对称。
'简化了intDaysDiff的计算。
'已从YearsDiff()重命名为Years()。
' 2000-12-18. 介绍了cbytMonthDaysMax。
' 2007-06-22. 版本2。完全重写。
'使用DateAdd()执行的2月底检查'
“在马库斯·G·费舍尔的想法之后。
Dim intDiff作为整数
整数形式的整数符号
整数形式的整数
'查找日历年的差异。
intYears=DateDiff(“yyyy”,datDate1,datDate2)
“积极响应。负间隔,检查第二个日期
在穿越日期之前、当天或之后的整整12个月内
同时对2月29日进行修正。闰年。
如果DateDiff(“d”,datDate1,datDate2)>0,则
intSign=Sgn(DateDiff(“d”,DateAdd(“yyyy”,INTYERS,DATDATATE1),DATATE2))
intDiff=Abs(intSign<0)
其他的
intSign=Sgn(日期差异(“d”,日期添加(“yyyy”,-intYears,datDate2),datDate1))
如果输入0,那么
'如有要求,将年的负计数抵消为连续序列。
intDiff=Abs(booLinear)
如果结束
intDiff=intDiff-Abs(intSign<0)
如果结束
'返回年数作为完整12个月期间的计数。
年份=整数年-整数年
端函数
查看DateDiff
应该做你想做的事
Public Function Years( _
ByVal datDate1 As Date, _
ByVal datDate2 As Date, _
Optional ByVal booLinear As Boolean) _
As Integer
' Returns the difference in full years 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 year counts.
' For a given datDate1, if datDate2 is decreased step wise one year 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 years to dates of Feb. 29.
' when the resulting year is a common year.
'
' 2000-11-03. Cactus Data ApS, CPH.
' 2000-12-16. Leap year correction modified to be symmetrical.
' Calculation of intDaysDiff simplified.
' Renamed from YearsDiff() to Years().
' 2000-12-18. Introduced cbytMonthDaysMax.
' 2007-06-22. Version 2. Complete rewrite.
' Check for month end of February performed with DateAdd()
' after idea of Markus G. Fischer.
Dim intDiff As Integer
Dim intSign As Integer
Dim intYears As Integer
' Find difference in calendar years.
intYears = DateDiff("yyyy", datDate1, datDate2)
' For positive resp. negative intervals, check if the second date
' falls before, on, or after the crossing date for a full 12 months 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("yyyy", intYears, datDate1), datDate2))
intDiff = Abs(intSign < 0)
Else
intSign = Sgn(DateDiff("d", DateAdd("yyyy", -intYears, datDate2), datDate1))
If intSign <> 0 Then
' Offset negative count of years to continuous sequence if requested.
intDiff = Abs(booLinear)
End If
intDiff = intDiff - Abs(intSign < 0)
End If
' Return count of years as count of full 12 months periods.
Years = intYears - intDiff
End Function