Office中国论坛/Access中国论坛

标题: 【分享】常用日期公式 [打印本页]

作者: andymark    时间: 2011-11-15 20:47
标题: 【分享】常用日期公式
  1. Public Function FstDayOfMth(InDate As Date) As Date
  2. FstDayOfMth = DateSerial(Year(InDate), Month(InDate), 1)
  3. End Function

  4. Public Function FstDayOfNextMnth(InDate As Date) As Date
  5. FstDayOfNextMnth = DateSerial(Year(InDate), Month(InDate) + 1, 1)
  6. End Function

  7. Public Function LstDayMnth(InDate As Date) As Date
  8. LstDayMnth = DateSerial(Year(InDate), Month(InDate) + 1, 0)
  9. End Function

  10. Public Function LstDayNextMnth(InDate As Date) As Date
  11. LstDayNextMnth = DateSerial(Year(InDate), Month(InDate) + 2, 0)
  12. End Function

  13. Public Function FstDayPrevMnth(InDate As Date) As Date
  14. FstDayPrevMnth = DateSerial(Year(InDate), Month(InDate) - 1, 1)
  15. End Function

  16. Public Function LstDayPrevMnth(InDate As Date) As Date
  17. LstDayPrevMnth = DateSerial(Year(InDate), Month(InDate), 0)
  18. End Function

  19. Public Function FstDayCurQtr(InDate As Date) As Date
  20. FstDayCurQtr = DateSerial(Year(InDate), Int((Month(InDate) - 1) / 3) * 3 + 1, 1)
  21. End Function

  22. Public Function LstDayCurQtr(InDate As Date) As Date
  23. LstDayCurQtr = DateSerial(Year(InDate), Int((Month(InDate) - 1) / 3) * 3 + 4, 0)
  24. End Function

  25. Public Function FstDayCurWeek(InDate As Date) As Date
  26. FstDayCurWeek = InDate - Weekday(InDate) + 1
  27. End Function

  28. Public Function LstDayCurWeek(InDate As Date) As Date
  29. LstDayCurWeek = InDate - Weekday(InDate) + 7
  30. End Function

  31. Public Function FstDayCurWeek2(InDate As Date, InStartWeek As Integer) As Date
  32. 'The first day of the current week (using settings in Options dialog box):
  33. FstDayCurWeek2 = InDate - Weekday(InDate, InStartWeek) + 1
  34. End Function

  35. Public Function LstDayCurWeek2(InDate As Date, InStartWeek As Integer) As Date
  36. 'The last day of the current week:
  37. LstDayCurWeek2 = InDate - Weekday(InDate, 0) + 7
  38. End Function

  39. Public Function FstWeekDayOfMth(InDate As Date, DayNum As Integer) As Date
  40. Dim FirstDay As Date
  41. Dim FirstWeekDay As Integer
  42. FirstDay = DateSerial(Year(InDate), Month(InDate), 1)
  43. FirstWeekDay = Weekday(FirstDay)
  44. Select Case FirstWeekDay
  45.     Case Is < DayNum
  46.         FstWeekDayOfMth = FirstDay + DayNum - FirstWeekDay
  47.     Case Is = DayNum
  48.         FstWeekDayOfMth = FirstDay
  49.     Case Else
  50.         FstWeekDayOfMth = FirstDay + DayNum - FirstWeekDay + 7
  51. End Select

  52. End Function

复制代码

作者: ly    时间: 2011-11-15 21:07
感谢楼主的无私奉献!
作者: yanwei82123300    时间: 2011-11-16 08:21
感谢楼主的无私奉献!
作者: 轻风    时间: 2011-11-16 09:40
可惜没个说明
作者: jinzhanxi    时间: 2011-11-16 10:13
看不懂,能解释下不
作者: changweiren    时间: 2011-12-3 14:24
感谢楼主分享




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3