IIf 语句查看月份
IIf Statement Looking at Months
我想创建一个 IIf 语句来计算 child 的预计离开日期。
例如,31/08 之前出生的 child 预计在 4 年后离开托儿所,而在该日期之后出生的 child 预计在 5 年后离开托儿所。
现在我想做的是询问一份 IIF 声明,该声明查看出生日期并决定是计算 4 年还是 5 年。但是,我一直 运行 对我正在使用的代码存在问题,即
= IIf([Date of Birth]>#31/08/0000# , =DateAdd("yyyy",4,[Date of Birth]) , =DateAdd("yyyy",5,[Date of Birth]))
因为有多个 children 的出生日期不同。需要有一种方法来专门查看月份。
编辑:
事实证明这不是我老板需要的,他需要的基本上是在 child 离开托儿所时显示,即当新学期到来并且 child 4 岁时。如果child是9月份之前出生的,那年就可以开学了。如果他不是 child 适用于明年 9 月份开学。
现在我不知道该怎么做,因为我尝试执行 IIF 函数的尝试完全失败了。有人可以帮忙吗?
试试:
=DateAdd("yyyy", IIf([Date of Birth] > DateSerial(Year([Date of Birth]), 8, 31), 4, 5), [Date of Birth])
编辑 1:
您可以像这样使用 DateAdd:
=IIf(DateAdd("yyyy", 4, [Date of Birth]) < DateSerial(Year(Date()), 9, 1), "Start school this year", "Postpone school start")
编辑 2:
或者您可以计算 children 在 9 月 1 日的年龄:
AgeAtSeptember: Age([Date of Birth], DateSerial(Year(Date()), 9, 1))
使用此功能:
' Returns the difference in full years from DateOfBirth to current date,
' optionally to another date.
' Returns zero if AnotherDate is earlier than DateOfBirth.
'
' Calculates correctly for:
' leap years
' dates of 29. February
' date/time values with embedded time values
' any date/time value of data type Date
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28th when adding a count of years to dates of Feb. 29th
' when the resulting year is a common year.
'
' 2015-11-24. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function Age( _
ByVal DateOfBirth As Date, _
Optional ByVal AnotherDate As Variant) _
As Integer
Dim ThisDate As Date
Dim Years As Integer
If IsDateExt(AnotherDate) Then
ThisDate = CDate(AnotherDate)
Else
ThisDate = Date
End If
' Find difference in calendar years.
Years = DateDiff("yyyy", DateOfBirth, ThisDate)
If Years > 0 Then
' Decrease by 1 if current date is earlier than birthday of current year
' using DateDiff to ignore a time portion of DateOfBirth.
If DateDiff("d", ThisDate, DateAdd(IntervalSetting(DtInterval.dtYear), Years, DateOfBirth)) > 0 Then
Years = Years - 1
End If
ElseIf Years < 0 Then
Years = 0
End If
Age = Years
End Function
我想创建一个 IIf 语句来计算 child 的预计离开日期。
例如,31/08 之前出生的 child 预计在 4 年后离开托儿所,而在该日期之后出生的 child 预计在 5 年后离开托儿所。
现在我想做的是询问一份 IIF 声明,该声明查看出生日期并决定是计算 4 年还是 5 年。但是,我一直 运行 对我正在使用的代码存在问题,即
= IIf([Date of Birth]>#31/08/0000# , =DateAdd("yyyy",4,[Date of Birth]) , =DateAdd("yyyy",5,[Date of Birth]))
因为有多个 children 的出生日期不同。需要有一种方法来专门查看月份。
编辑: 事实证明这不是我老板需要的,他需要的基本上是在 child 离开托儿所时显示,即当新学期到来并且 child 4 岁时。如果child是9月份之前出生的,那年就可以开学了。如果他不是 child 适用于明年 9 月份开学。 现在我不知道该怎么做,因为我尝试执行 IIF 函数的尝试完全失败了。有人可以帮忙吗?
试试:
=DateAdd("yyyy", IIf([Date of Birth] > DateSerial(Year([Date of Birth]), 8, 31), 4, 5), [Date of Birth])
编辑 1:
您可以像这样使用 DateAdd:
=IIf(DateAdd("yyyy", 4, [Date of Birth]) < DateSerial(Year(Date()), 9, 1), "Start school this year", "Postpone school start")
编辑 2:
或者您可以计算 children 在 9 月 1 日的年龄:
AgeAtSeptember: Age([Date of Birth], DateSerial(Year(Date()), 9, 1))
使用此功能:
' Returns the difference in full years from DateOfBirth to current date,
' optionally to another date.
' Returns zero if AnotherDate is earlier than DateOfBirth.
'
' Calculates correctly for:
' leap years
' dates of 29. February
' date/time values with embedded time values
' any date/time value of data type Date
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28th when adding a count of years to dates of Feb. 29th
' when the resulting year is a common year.
'
' 2015-11-24. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function Age( _
ByVal DateOfBirth As Date, _
Optional ByVal AnotherDate As Variant) _
As Integer
Dim ThisDate As Date
Dim Years As Integer
If IsDateExt(AnotherDate) Then
ThisDate = CDate(AnotherDate)
Else
ThisDate = Date
End If
' Find difference in calendar years.
Years = DateDiff("yyyy", DateOfBirth, ThisDate)
If Years > 0 Then
' Decrease by 1 if current date is earlier than birthday of current year
' using DateDiff to ignore a time portion of DateOfBirth.
If DateDiff("d", ThisDate, DateAdd(IntervalSetting(DtInterval.dtYear), Years, DateOfBirth)) > 0 Then
Years = Years - 1
End If
ElseIf Years < 0 Then
Years = 0
End If
Age = Years
End Function