Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a function to determine # days in a month?

Status
Not open for further replies.

JennyPeters

Programmer
Oct 29, 2001
228
0
0
US
Is there a function to determine # days in a month?

Thanks,

Jenny
 
I don't know of any built-in function that does that.

What I do to determine the last day of the month is:
Starting with the first day of the month, Add one month, then subtract one day.

LastDate = DateAdd("D", -1, DateAdd("M", 1, FirstDate))
 
Ahem....

DateDiff("d", #7/1/2001#, #8/1/2001#)

This returns the number of days. Simply put the first of the next month and perform the datediff.

Gary
gwinn7
 
i made a form with two text boxes, txtMonth and txtYear.
added a button to launch this code OnClick.
You can set it up differently, but this should give you a good start. Essentially the number of days in a month = the last day in the month. you can determine this by using DateSerial, with DAY set to 0. check out DateSerial in Help for details.

in the button's OnClick event, put:

Code:
Dim intMonth, intYear, LastDayOfThisMonth, NoOfDays
    
    intMonth = Me.txtMonth
    intYear = Me.txtYear
    
    LastDayOfThisMonth = DateSerial(intYear, intMonth + 1, 0)
    NoOfDays = Day(LastDayOfThisMonth)
    
    MsgBox NoOfDays

So in the form, enter in a month (number) in the month field and a year in the year field and hit the button.

g
 
For some date within the month("MyDate"):

? Day(DateSerial(Year(MyDate), Month(MyDate) + 1, 0)), as in

MyDate = Now 'on 11/12/2001
? Day(DateSerial(Year(MyDate), Month(MyDate) + 1, 0))
30

MyDate = #9/1/2001#
? Day(DateSerial(Year(MyDate), Month(MyDate) + 1, 0))
30

MyDate = #10/1/2001#
? Day(DateSerial(Year(MyDate), Month(MyDate) + 1, 0))
31

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Hi Jenny,
Try this
In Module. add new

put in the code below
Function DaysofMonth(mydate)
If IsDate(mydate) Then
If DatePart("m", mydate) = 1 Then
DaysofMonth = 31
ElseIf DatePart("m", mydate) = 2 Then
If Int(DatePart("yyyy", mydate) / 4) - DatePart("yyyy", mydate) = 0 Then
DaysofMonth = 29
Else
DaysofMonth = 28
End If
ElseIf DatePart("m", mydate) = 3 Then
DaysofMonth = 31
ElseIf DatePart("m", mydate) = 4 Then
DaysofMonth = 30
ElseIf DatePart("m", mydate) = 5 Then
DaysofMonth = 31
ElseIf DatePart("m", mydate) = 6 Then
DaysofMonth = 30
ElseIf DatePart("m", mydate) = 7 Then
DaysofMonth = 31
ElseIf DatePart("m", mydate) = 8 Then
DaysofMonth = 31
ElseIf DatePart("m", mydate) = 9 Then
DaysofMonth = 30
ElseIf DatePart("m", mydate) = 10 Then
DaysofMonth = 31
ElseIf DatePart("m", mydate) = 11 Then
DaysofMonth = 30
ElseIf DatePart("m", mydate) = 12 Then
DaysofMonth = 31
End If
End If


Use it where ever needed eg
on a command button

like
Dim DinMonth As Integer

DinMonth = DaysofMonth(mydate)
 
I just ran a quick test Mr Reds function Vs Gerryks

I did edit Gerry's some to read

Function Gerrys(mydate) As Integer
If IsDate(mydate) Then
Select Case DatePart("m", mydate)
Case 1, 3, 5, 7, 8, 10, 12
Gerrys = 31
Case 4, 6, 9, 11
Gerrys = 30
Case 2
If Int(DatePart("yyyy", mydate) / 4) - DatePart("yyyy", mydate) = 0 Then
Gerrys = 29
Else
Gerrys = 28
End If
End Select
End If
End Function

and Mikes

Function mikes(mydate As Date) As Integer
mikes = Day(DateSerial(Year(mydate), month(mydate) + 1, 0))
End Function

now the test
? mikes(now())
30
? gerrys(now())
30
? mikes(#2/12/2000#)
29
? gerrys(#2/12/2000#)
28

you be the Judge !!!
 
Hi Jenny,
Missed a divide by 4
Case 2
If Int(DatePart("yyyy", mydate) / 4) - DatePart("yyyy", mydate)/4 = 0 Then
Gerrys = 29
Else
Gerrys = 28
Sorry but Mike's look neater anyway

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top