以下代码返回特定数据库的年龄。
我可以添加什么来获取年,月,日的确切日期?
<%= DateDiff("yyyy",rs("Dateofbirth"),date)%>
即。结果应该是12岁6个月8天。
答案 0 :(得分:3)
DateDiff(“d”,rs(“Dateofbirth”),日期)将为您提供若干天。 DateDiff(“m”,rs(“Dateofbirth”),日期)会给你几个月。
所以,像(我不确切知道什么是rs()):
CurrentDate = rs("Dateofbirth")
Years = DateDiff("yyyy", CurrentDate ,date)
ThisYear = DateAdd("yyyy", Years, CurrentDate)
Months = DateDiff("m", ThisYear ,date)
ThisMonth = DateAdd("m", Months, CurrentDate)
Days = DateDiff("d", ThisMonth, date)
Age = CStr(Years) & " years" & CStr(Months) & " months" & CStr(Days) & Days
但经过一些玩弄,它并不总是奏效。差异有点四舍五入。因此,在某些情况下,我有数天或数月的负数。所以我疯了,过度保护了代码:
MsgBox(Age("09-12-1946"))
Function Age(DateOfBirth)
Dim CurrentDate, Years, ThisYear, Months, ThisMonth, Days
CurrentDate = CDate(DateOfBirth)
Years = DateDiff("yyyy", CurrentDate, Date)
ThisYear = DateAdd("yyyy", Years, CurrentDate)
Months = DateDiff("m", ThisYear, Date)
ThisMonth = DateAdd("m", Months, ThisYear)
Days = DateDiff("d", ThisMonth, Date)
Do While (Days < 0) Or (Months < 0)
If Days < 0 Then
Months = Months - 1
ThisMonth = DateAdd("m", Months, ThisYear)
Days = DateDiff("d", ThisMonth, Date)
End If
If Months < 0 Then
Years = Years - 1
ThisYear = DateAdd("yyyy", Years, CurrentDate)
Months = DateDiff("m", ThisYear, Date)
ThisMonth = DateAdd("m", Months, ThisYear)
Days = DateDiff("d", ThisMonth, Date)
End If
Loop
Age = Years & "y/" & Months & "m/" & Days
End Function
这可能是过于防守的代码(有许多重复的行,坏的坏的坏),但它有效。我让你变得更漂亮。或者在Code Review上查询更好的代码。
答案 1 :(得分:1)
这是具体代码的最终答案
function getAge(DateOfBirth) &#39;多年的年龄 ageyears = DateDiff(&#34; yyyy&#34;,DateOfBirth,date())
'get month of dob
BirthMonth = Month(DateOfBirth)
'get todays month
CurrMonth = Month(date)
'get day of dob
ageDay = Day(Date)
If CurrMonth< BirthMonth Then
Agemonth= 12 + CurrMonth - BirthMonth
else
agemonth = CurrMonth - BirthMonth
end if
Age= ageyears&"years "&ageMonth&" months "&ageDay& "days"
getAge =Age
结束功能
感谢大家的帮助
答案 2 :(得分:0)
也许最快最简单的方法是将日期差异除以365.25,如下所示:
Dim age, dob
dob = CDate("01-Jan-1980")
age = (Now() - dob) / 365.2425
虽然它不准确,但它很可能足够准确99 .99 %的使用案例(即你不需要计算到第二个)。
答案 3 :(得分:0)
根据年龄计算,在其他问题中提出了更准确的年龄检查。
Dim Age, CurrentDate, BirthDate
CurrentDate = Now()
BirthDate = CDate(rs("Dateofbirth") & "")
'Convert to yyymmdd
CurrentDate = Year(CurrentDate) & Right("00" & Month(CurrentDate), 2) & Right("00" & Day(CurrentDate), 2)
BirthDate = Year(BirthDate ) & Right("00" & Month(BirthDate ), 2) & Right("00" & Day(BirthDate ), 2)
Age = Fix((0 + CurrentDate - BirthDate) / 10000)
Response.Write Age
答案 4 :(得分:0)
请尝试以下代码:
' Usage:
' GetDiff(date1, date2, accuracy)
' date1 - first date
' date2 - second date
' accuracy - output format: from 1 (N years) to 6 (N years, N months, N days, N hours, N minutes, N seconds)
MsgBox GetDiff(CDate("09-11-2014 10:55:30"), CDate("08-10-2016 15:45:10"), 6)
Function GetDiff(d1, d2, a)
Dim r
ReDim r(a - 1)
Delta 0, Array("yyyy", "m", "d", "h", "n", "s"), Array("years", "months", "days", "hours", "minutes", "seconds"), r, d1, d2, False
GetDiff = Join(r, ", ")
End Function
Sub Delta(i, t, n, r, d1, d2, c)
Dim q, d
q = DateDiff(t(i), d1, d2)
If UBound(r) > i Then
Do
d = DateAdd(t(i), q, d1)
Delta i + 1, t, n, r, d, d2, c
If c Then Exit Do
q = q - 1
Loop
End If
c = q >= 0
r(i) = q & " " & n(i)
End Sub