有什么函数可以得到一个月的天数?

woodheart 2000-09-04 02:12:00
加精
比如8月31天
9月30天

谢了
...全文
1003 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
shines77 2000-09-05
  • 打赏
  • 举报
回复
nanman的代码最好应为:

Public Function DaysOfMonth(Year As Long, Month As Long) As Long
Dim Day1 As String, Day2 As String
Day1 = CStr(Year) + "-" + CStr(Month) + "-1"
Day2 = CStr(Year) + "-" + CStr(Month + 1) + "-1"
DaysOfMonth = DateDiff("d", Day1, Day2)
End Function

当然用变体的也对,但用Long锁定类型便于运算,减少出错。
shines77 2000-09-05
  • 打赏
  • 举报
回复
不错,不错,想法不错,huntout改得很对!
huntout 2000-09-05
  • 打赏
  • 举报
回复
to mxp:
想法不錯,但是你的程序好像有點問題唷!︰)

Public Function DaysOfMonth(aYear As Integer, aMonth As Integer) As Integer
Dim i As Integer
For i = 31 To 28 Setp -1
If IsDate(CStr(aYear) & "-" & CStr(aMonth) & "-" & i) Then
DaysOfMonth = i
Exit Function
End If
Next
End Function
mxp 2000-09-05
  • 打赏
  • 举报
回复
呵呵!还有一种方法的
Public Function DaysOfMonth(aYear As Integer, aMonth As Integer) As Integer
For i = 1 To 31
ine = IsDate(CStr(aYear) & "-" & CStr(aMonth) & "-" & i)
If ine = False Then
DaysOfMonth = i - 1
Exit Function
End If
Next
If ine = True Then DaysOfMonth = i - 1
End Function
youngeryang 2000-09-05
  • 打赏
  • 举报
回复
一行代码就行:
DateDiff("d",DateSerial(datYear,datMonth,1), DateAdd("m", 1,DateSerial(datYear,datMonth,1)))
其中datYear,datMonth是要计算的年和月。使用DateAdd可以保证计算12月时正确。
mxp 2000-09-05
  • 打赏
  • 举报
回复
to huntout:
ok!您的写法确实省去了不少不必要的循环。非常好!!
我是从以前作的打印月每天报表的程序中摘录的,思路被以前的需求束缚了,所以对于返回天数就没想到能进行优化。

呵呵!用下面的语句判断也可以:
Public Function DaysOfMonth(aYear As Integer, aMonth As Integer) As Integer
On Error Resume Next
For i = 31 To 28 Step -1
y = Month(CStr(aYear) & "-" & CStr(aMonth) & "-" & i)
If y = aMonth Then
DaysOfMonth = i
Exit Function
End If
Next
End Function
bing71 2000-09-04
  • 打赏
  • 举报
回复
DateDiff("d", "2000/05/01", "2000/06/01")可得出2000年5月的天数,以此类推。
huntout 2000-09-04
  • 打赏
  • 举报
回复
我認為wwwunix的方法最直接最快!
閏年算法
((year mod 4 = 0) and (year mod 100 <> 0)) or (year mod 400 = 0)
  • 打赏
  • 举报
回复
VB里没有这样的函数,自己写一个吧。例如:

Public Function DaysOfMonth(Year As Integer, Month As Integer)
Dim Day1, Day2 As String
Day1 = CStr(Year) + "-" + CStr(Month) + "-1"
Day2 = CStr(Year) + "-" + CStr(Month + 1) + "-1"
DaysOfMonth = DateDiff("d", Day1, Day2)
End Function
  • 打赏
  • 举报
回复
VB里没有这样的函数,自己写一个吧。例如:

Public Function DaysOfMonth(Year As Integer, Month As Integer)
Dim Day1, Day2 As String
Day1 = CStr(Year) + "-" + CStr(Month) + "-1"
Day2 = CStr(Year) + "-" + CStr(Month + 1) + "-1"
DaysOfMonth = DateDiff("d", Day1, Day2)
End Function
wwwunix 2000-09-04
  • 打赏
  • 举报
回复
自已写一个嘛:
if (month in (1,3,5,7,8,10,12))
return 31;
else if (month in(4,6,9,11))
return 30;
else if (year 是闰年)
return 29;
else
return 28;
闰年的算法到处都有,就不写出来了。
masterkey 2000-09-04
  • 打赏
  • 举报
回复

我认为最简单的办法就是:
用下一个第一天减1就可以得到本月最后一天,比如要算8月份的天数,就以9月1日减1得到8月份31日,那么8月就是31天。
Dim vDate As Date
Dim mm, yy, dd As Integer
yy = Year(Date)
mm = Month(Date)
If mm = 12 Then
yy = yy + 1
mm = 1
Else
mm = mm + 1
End If
vDate = DateSerial(yy, mm, 1) - 1
dd = Day(vDate)
woodheart 2000-09-04
  • 打赏
  • 举报
回复
DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])

返回两个日期之间的时间间隔数目

7,762

社区成员

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

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