Office中国论坛/Access中国论坛

标题: [分享]求某月的最后一天的函数 [打印本页]

作者: zhuyiwen    时间: 2002-11-5 00:30
标题: [分享]求某月的最后一天的函数
求某月的最后一天,我觉得这样更完美。

这是我的原创代码。
  1. Public Function MonLastDay(dFirst As Date) As Date
  2.     Dim dm As Date
  3.     Dim m As Integer
  4.    
  5.     dm = dFirst
  6.     m = Month(dm)
  7.     Do While Month(dm) = m
  8.        dm = dm + 1
  9.     Loop
  10.     MonLastDay = dm - 1
  11. End Function
复制代码
AlexLiu
  1. Public Function MonLastDay1(dFirst As Date) As Date
  2.     MonLastDay1 = DateSerial(Year(dFirst), Month(dFirst) + 1, 1) - 1
  3. End Function
复制代码
李啸林
  1. Public Function MonLastDay1(dFirst As Date) As Date
  2.     MonLastDay1 = DateAdd("d", -1, CDate(DatePart("yyyy", DateAdd("m", 1, dFirst)) & "-" & DatePart("m", DateAdd("m", 1, dFirst))))
  3. End Function
复制代码

作者: WTM1    时间: 2002-11-5 23:08
很好!
谢谢!
作者: 李啸林    时间: 2002-11-6 18:45
我这样写,你们觉得如何?

Public Function MonLastDay1(dFirst As Date) As Date

MonLastDay1 = DateAdd("d", -1, CDate(DatePart("yyyy", DateAdd("m", 1, dFirst)) & "-" & DatePart("m", DateAdd("m", 1, dFirst))))

End Function

作者: zhuyiwen    时间: 2002-11-6 21:30
非常好!

用这个
CDate(DatePart("yyyy", DateAdd("m", 1, dFirst)) & "-" & DatePart("m", DateAdd("m", 1, dFirst)))
获取下一月的第一天!!!高招![em26]

作者: yahai    时间: 2002-11-6 22:30
好啊
作者: AlexLiu    时间: 2002-11-6 23:35
Public Function MonLastDay1(dFirst As Date) As Date

MonLastDay1 = DateSerial(Year(dFirst), Month(dFirst) + 1, 1) - 1

End Function

效果类似,不过更简单一些,同样适用于任何一个月。

作者: Trynew    时间: 2002-11-7 00:01
用DateSerial容易处理12月,是比CDATE好。Good!
作者: HG    时间: 2002-11-7 01:40
http://www.office-cn.net/bbs/dispbbs.asp?boardID=5&RootID=19834&ID=19834
特別處理過的TSQL寫的同功能函數。
作者: zyz    时间: 2002-11-7 02:26
以下是引用AlexLiu在2002-11-6 15:35:03的发言:
Public Function MonLastDay1(dFirst As Date) As Date

MonLastDay1 = DateSerial(Year(dFirst), Month(dFirst) + 1, 1) - 1 

End Function

效果类似,不过更简单一些,同样适用于任何一个月。

原来如此:
DateSerial(2002,15,1) = 2002-3-1[em26]
作者: AlexLiu    时间: 2002-11-7 16:13
DateSerial(2002,15,1) = 2002-3-1


你写错了,应该是:DateSerial(2002,15,1) = 2003-3-1
作者: zyz    时间: 2002-11-7 17:16
对不起,一时兴奋,写错了. [em26]
作者: 大熊    时间: 2002-11-7 18:49
AlexLiu,一高手也。此法不容易想到。




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