Date 计算最近的周年纪念日

Date 计算最近的周年纪念日,date,ms-access,vba,Date,Ms Access,Vba,我正在写一个训练数据库 学生们在公司开始工作的那天有一个服务日期 我需要知道他们是否从服役日期算起,在过去的一年里参加过某一课程。不是日历年,参加某些课程的需要在周年纪念日重新设置…因此,每个学生的到期日都不同 我想我可以做这样的事情……但我相信有一种更简单的方法: iDiff = cint((Date() - ServiceDate)/365) 'Gives the number of years between the ServiceDate and today dMostRecent =

我正在写一个训练数据库

学生们在公司开始工作的那天有一个服务日期

我需要知道他们是否从服役日期算起,在过去的一年里参加过某一课程。不是日历年,参加某些课程的需要在周年纪念日重新设置…因此,每个学生的到期日都不同

我想我可以做这样的事情……但我相信有一种更简单的方法:

iDiff = cint((Date() - ServiceDate)/365) 'Gives the number of years between the ServiceDate and today
dMostRecent = DateAdd("yyyy",iDiff, ServiceDate) 'Uses the mm/dd from ServiceDate, but makes it the current year
If dMostRecent > Date() then 'If the new date is in the future...
  dMostRecent = DateAdd("yyyy",-1,dMostRecent) 'Subtract 1 year
End If
'dMostRecent should now be within the last 12 months and I can check the dates that the given course was last taken and compare

有没有一种更简单的方法可以做到这一点……或者有人认为这种方法有问题吗?处理日期有时会很痛苦…

这种方法可能更准确一些。不确定它是否更快,但它应该有助于避免您的闰年问题。除非最初的服务日期是闰年。。那你就得加减一天

If (DatePart("m", ServiceDate) = 2 And DatePart("d", ServiceDate) = 29) Then
  ServiceDate = DateAdd("d", -1, ServiceDate)
End If   

dMostRecent = DatePart("m", ServiceDate) & "-" & DatePart("m", ServiceDate) & "-" & DatePart("yyyy", Date)

If dMostRecent > Date Then
    dMostRecent = DateAdd("yyyy", -1, dMostRecent)
End If

我不确定我是否完全理解你想要什么,但我怀疑DateSerial可能有用

公共函数LastAnniversaryDateByVal pServiceDate As Date As Date As Date Dim dtethis year As Date 变暗数据返回为日期 dteThisYear=DateSerialYearDate、MonthServiceDate、DaypServiceDate 如果数据年份>日期,则 dteReturn=DateAddyyyy,-1,dtethiswear 其他的 dteReturn=dtethis year 如果结束 LastAnniversaryDate=dteReturn 端函数 闰年被认为是一个可能的复杂因素。DateSerial可以按照您的意愿处理。例如,由于2015年不是闰年,DateSerial2015,2,29返回2015年1月3日。这意味着LastAnniversaryDate 2/29/2012将为您提供2015年3月1日


如果您想将2月28日作为2月29日服务日期的周年纪念日,则需要修改此功能。

Leapyears的天数超过365天非常正确,但它仅相差1天,可能是一年的零头。我认为这没什么大不了的…但我可能错了。dServiceDate=DLookupServiceDate,tblUserList,NTID='&sNTID&'dReturn=DateSerialYearDate,MonthdServiceDate,dayservicedate如果dReturn>Date,那么dReturn=dateaddyyyyy,-1,dReturn我只缩短到这三行…很好而且紧凑。根据多个日期、闰年和非闰年对其进行测试。我最初的代码,闰年在那个时候只是个问题。2月底,3月初。这似乎效果更好。谢谢