按日期计算岁数?在线等待,答对马上给分

kyzh 2004-01-06 05:11:39
一个日期跟系统日期对比,算出人的岁数是多少岁几个月几日
例如:2002年12月1日和2004年1月6日比较,得到一岁零一个月五日,代码怎么写?
...全文
178 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
jone999 2004-01-07
  • 打赏
  • 举报
回复
http://www.1000vb.net/html/gb/exp2/QQExp053.html#datediff
88391788 2004-01-07
  • 打赏
  • 举报
回复
没问题
kmzs 2004-01-07
  • 打赏
  • 举报
回复
这是什么问题
lanslotliu 2004-01-07
  • 打赏
  • 举报
回复
如何用SQL实现呢??
不如在sql server服务端实现。
northwolves 2004-01-07
  • 打赏
  • 举报
回复
没想到这个问题还挺复杂的,主要是概念的模糊,一时难以说清。

2003年2月28日到2003年3月1日: 1天

2003年2月28日到2003年3月28日: 1月?
hxy2003 2004-01-07
  • 打赏
  • 举报
回复
呵呵
SoHo_Andy 2004-01-07
  • 打赏
  • 举报
回复
可能不符合的
楼主还问过我怎么判断闰年闰月等细节问题

他的要求 短时间是比较难全部实现的
rainstormmaster 2004-01-07
  • 打赏
  • 举报
回复
探讨,同northwolves(野性的呼唤)、楼主:
我写的函数的结果和northwolves(野性的呼唤)是一样的,我就不贴了,大同小异,我的问题是:
Private Sub Command1_Click()
MsgBox age(CDate("2002年10月31日"))
MsgBox age(CDate("2002年11月30日"))
MsgBox age(CDate("2002年12月31日"))
End Sub

2002年10月31日到2002年11月30日,及2002年11月30日到2002年12月31日到底算不算一个整月?

现在,我是糊涂了。

如果,答案符合楼主要求的话,就什么都不用说了,如果不是,这个问题就复杂了。
wsd309 2004-01-07
  • 打赏
  • 举报
回复
Private Function Sjcz(id As Date, dqsj As Date, rq As String)


If Day(dqsj) - Day(id) >= 0 Then
ts = Day(dqsj) - Day(id)
If Month(dqsj) - Month(id) > 0 Then
ns = Year(dqsj) - Year(id)
ys = Month(dqsj) - Month(id)
Else
ys = (12 - Month(dqsj)) + Month(id)
ns = Year(dqsj) - Year(id) - 1
End If

Else
If Month(dqsj) - Month(id) > 0 Then
ns = Year(dqsj) - Year(id)
Select Case Month(id)
Case 1, 3, 5, 7, 8, 10, 12
ts = 31 - Day(id) + Day(dqsj)
Case 4, 6, 9, 11
ts = 30 - Day(id) + Day(dqsj)
Case 2
nd = Year(id) Mod 4
If nd = 0 Then
ts = 29 - Day(id) + Day(dqsj)
Else
ts = 28 - Day(id) + Day(dqsj)
End If
End Select
ys = Month(dqsj) - Month(id) - 1
Else
ns = Year(dqsj) - Year(id) - 1
Select Case Month(id)
Case 1 Or 3 Or 5 Or 7 Or 8 Or 10 Or 12
ts = 31 - Day(id) + Day(dqsj)
Case 4 Or 6 Or 9 Or 11
ts = 30 - Day(id) + Day(dqsj)
Case 2
nd = Year(id) Mod 4
If nd = 0 Then
ts = 29 - Day(id) + Day(dqsj)
Else
ts = 28 - Day(id) + Day(dqsj)
End If
End Select
ys = (12 - Month(dqsj)) + Month(id) - 1
End If
End If

rq = CStr(ns) & " 年 " & CStr(ys) & " 个月 " & CStr(ts) & " 天 "

End Function
northwolves 2004-01-07
  • 打赏
  • 举报
回复
修正后也不对,重写:

Private Sub Command1_Click()
MsgBox age(CDate("1902年12月1日"))
MsgBox age(CDate("2004年1月8日"))
'MsgBox age(CDate("1912年10月23日"))
End Sub
Function age(ByVal birthday As Date) As String
If birthday > Date Then MsgBox "err!you can't be born in the future!": Exit Function
Dim nday As Integer, nmonth As Integer, nyear As Integer ' today
Dim bday As Integer, bmonth As Integer, byear As Integer 'birthday
Dim theday As Integer, themonth As Integer, theyear As Integer 'datediff
nday = Day(Date)
nmonth = Month(Date)
nyear = Year(Date)
bday = Day(birthday)
bmonth = Month(birthday)
byear = Year(birthday)
If nday >= bday Then
theday = nday - bday
Else
theday = Format(DateSerial(Year(Date), Month(Date), nday - bday), "d")
nmonth = nmonth - 1
End If
If nmonth >= bmonth Then
themonth = nmonth - bmonth
Else
themonth = nmonth + 12 - bmonth
nyear = nyear - 1
End If
theyear = nyear - byear
age = theyear & " 岁 " & themonth & " 月 " & theday & " 天"

End Function
rainstormmaster 2004-01-06
  • 打赏
  • 举报
回复
to northwolves(野性的呼唤):

试试:
MsgBox age(CDate("1902年12月1日"))


to paoluo:
试试:
Debug.Print Format(DateSerial(0, 0, DateDiff("D", #12/1/1902#, #1/6/2004#)), "YY 岁 M月 D日")

修正:
Function age(ByVal birthday As Date) As String
Dim theday As Date
theday = DateSerial(Year(Date) - Year(birthday), Month(Date) - Month(birthday), Day(Date) - Day(birthday))
age = Format(theday, "yyyy岁零m月d天")
If Left(age, 1) = "0" Then age = Right(age, Len(age) - 1)
End Function
dazhuang1 2004-01-06
  • 打赏
  • 举报
回复
分开比较就可以了,提出年\月\日,然后和另一组比较,当然你得判断月份的大小,代码多了点,但我记得vb里有自带函数可以直接取得的
northwolves 2004-01-06
  • 打赏
  • 举报
回复
Private Sub Command1_Click()

MsgBox age(CDate("2002年12月1日"))
'MsgBox age(CDate("1994年7月12日"))
'MsgBox age(CDate("1912年10月23日"))
End Sub
Function age(ByVal birthday As Date) As String
Dim theday As Date
theday = DateSerial(Year(Date) - Year(birthday), Month(Date) - Month(birthday), Day(Date) - Day(birthday))
age = Format(theday, "yy岁零m月d天")
If Left(age, 1) = "0" Then age = Right(age, Len(age) - 1)
End Function
paoluo 2004-01-06
  • 打赏
  • 举报
回复
提点意见

eg:

Format(DateSerial(0, 0, DateDiff("D", #12/01/2002#, #01/06/2004#)), "YY 岁 M月 D日")

这个基本可行,很多日期比较都可以,但还是有误差,可以试着改改。

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧