随便给一个日期,怎样求这个日期所在月的最后一天,VB里有这样的函数吗?

snowerhuayun 2004-01-29 11:04:17
比如说我传一个日期
2004-1-5
这个日期的所在月的最后一天是31
我就想求这个31,怎么办?
...全文
248 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
paoluo 2004-01-29
  • 打赏
  • 举报
回复
MsgBox DatePart("d", DateSerial(Year("2004/01/05"), Month("2004/01/05") + 1, 1) - 1)

替换日期即可。
海牛 2004-01-29
  • 打赏
  • 举报
回复
呵呵,,不好意思,,上面的代码太累赘了,,改一下哦

Private Function GetLastDate(ByVal strDate As String) As String
Dim intYear As Integer
Dim intMonth As Integer
Dim strT As String
Dim I As Integer

intYear = Year(strDate)
intMonth = Month(strDate)

For I = 31 To 28 Step -1
strT = intYear & "-" & intMonth & "-" & I
If IsDate(strT) = True Then
GetLastDate = strT
Exit Function
End If
Next
End Function
海牛 2004-01-29
  • 打赏
  • 举报
回复
如果搂住觉得上面的方法不好,,试试俺的吧

Private Function GetLastDate(ByVal strDate As String) As String
Dim intYear As Integer
Dim intMonth As Integer
Dim strT As String

intYear = Year(strDate)
intMonth = Month(strDate)
strT = intYear & "-" & intMonth & "-31"
If IsDate(strT) = True Then
GetLastDate = strT
Exit Function
Else
strT = intYear & "-" & intMonth & "-30"
If IsDate(strT) = True Then
GetLastDate = strT
Exit Function
Else
strT = intYear & "-" & intMonth & "-29"
If IsDate(strT) = True Then
GetLastDate = strT
Exit Function
Else
strT = intYear & "-" & intMonth & "-28"
If IsDate(strT) = True Then
GetLastDate = strT
Exit Function
End If
End If
End If
End If
End Function
rainstormmaster 2004-01-29
  • 打赏
  • 举报
回复
//注意
如果月份是12时
要改一下年份

不用改
paoluo 2004-01-29
  • 打赏
  • 举报
回复
得到某个日期的所在月的最后一天的日期
Dim dtl As Date
Dim dt2 As Date
dt1 = Text1.Text
dt2 = DateSerial(Year(dt1), Month(dt1) + 1, 1) - 1
MsgBox dt2
xayzmb 2004-01-29
  • 打赏
  • 举报
回复
先取出日期的年和月,比如:日期是 2003-02-02
取出后为2003-02
将月加一,变为:2003-03
生成一个新日期:2003-03-01
将新日期减一
可以得到02月的最后一天
注意
如果月份是12时
要改一下年份
一个笨办法....
^_^
victorycyz 2004-01-29
  • 打赏
  • 举报
回复
dim dtA as date
dtA=#2004-1-5#
print datepart("d",dateadd("d",-datepart("d",dtA),dateadd("m",1,dtA)))
minajo21 2004-01-29
  • 打赏
  • 举报
回复
In many industries (particularly in the insurance industry), it注释:s important to know the last day of the month. To find the last day of a given month, add a text box and a command button to a form. Enter the following code in the command button:



Dim TEMP2 As Date

Dim nLastDay As Integer

TEMP2 = InputBox$("Please Enter A Date", "LastDay")

nLastDay = DatePart("d", DateAdd("M", 1, TEMP2 - DatePart("d", TEMP2)))

Text1.Text = nLastDay

When you run the application and click the button, you注释:ll be prompted for a date. Then, the program will display the last day of that month of any year.
rainstormmaster 2004-01-29
  • 打赏
  • 举报
回复
Private Function getlastday(ByVal mdate As Date) As Integer
Dim y As Integer, m As Integer
y = Year(mdate)
m = Month(mdate)
Dim d As Date
d = DateSerial(y, m + 1, 1)
d = DateAdd("d", -1, d)
getlastday = Day(d)
End Function

Private Sub Command2_Click()
'调用
MsgBox "当月最后一天的日期为:" + CStr(getlastday(CDate("2004年1月5日")))
End Sub
haipingma 2004-01-29
  • 打赏
  • 举报
回复
right(DateSerial(Year("2004-2-2"), Month("2004-2-2") + 1, 0),2)
2004-2-2 替換需要的日期即可
华芸智森 2004-01-29
  • 打赏
  • 举报
回复
lYear=已知的年.lMonth=已知的月.
MDates = 已知年份与月份的天数

MDates = Clng(Format$(DateSerial(lYear, lMonth+ 1, 1) - 1,"DD"))
kimurakenshin 2004-01-29
  • 打赏
  • 举报
回复
已知年份和月份了,那求这个月有多少天不就行了吗?
newsun2003 2004-01-29
  • 打赏
  • 举报
回复
Dim str1 As Date
Dim str2 As Date
str1 = Now
str2 = DateAdd("d", -1, Left(Format(DateAdd("m", 1, str1), "yyyy-mm-dd"), 7) & "-01")
MsgBox str2
rexyudl 2004-01-29
  • 打赏
  • 举报
回复
Dim nLastDay As Integer
'判断当月的最大天数
nLastDay = DateDiff("d", DateSerial(Year(Date), Month(Date), 1), DateAdd("m", 1, DateSerial(Year(Date), Month(Date), 1)))
northwolves 2004-01-29
  • 打赏
  • 举报
回复
Function lastday(ByVal mydate As Date) As Date
lastday = DateSerial(Year(mydate), Month(mydate) + 1, 0)
End Function
Private Sub Form_Load()
MsgBox lastday(#1/5/2004#)
End Sub
victorycyz 2004-01-29
  • 打赏
  • 举报
回复
刚才写的欠考虑,有点bug,改一下:

Dim dtA As Date
dtA = #1/5/2004#
dtA = DateAdd("m", 1, dtA)
Print "本月的天数是:" & Day(dtA - Day(dtA)) & "天"

7,763

社区成员

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

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