A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Number of days in a given month



 
 
Thread Tools Display Modes
  #1  
Old September 21st, 2004, 03:13 PM
Hallgeir
external usenet poster
 
Posts: n/a
Default Number of days in a given month

Is there a (easy) way to find the number of days in a month if I have the
date. Example: If the date field is 08.10.04, I want the Daysinmonth field
to show 31.

Thanks in advance!
Hallgeir


  #2  
Old September 21st, 2004, 04:19 PM
Chris Nebinger
external usenet poster
 
Posts: n/a
Default

It would be easier to do this in a function. Paste the
following into a regular module:

Note: No error handling:


Public Function NumberOfDaysInMonth(varDate)
If IsDate(varDate) Then
Dim intYear As Integer
Dim intMonth As Integer
Dim intDay As Integer
Dim dteLastDayOfMonth As Date

intYear = DatePart("yyyy", DateAdd("m", 1, varDate))
intMonth = DatePart("m", DateAdd("m", 1, varDate))
intDay = 1
dteLastDayOfMonth = DateSerial(intYear, intMonth,
intDay) - 1
NumberOfDaysInMonth= Day(dteLastDayOfMonth)
Else
NumberOfDaysInMonth= Null
End If
End Function

The logic goes: Add a month to he day in question, then
get the first of the month. Subtract one day, then figure
out the day part. This method works even for leap years.

Once you have this in a module, you can use it in a query.


DaysInMonth: NumberOfDaysInMonth([MyDateField])


Chris Nebinger


Chris Nebinger


-----Original Message-----
Is there a (easy) way to find the number of days in a

month if I have the
date. Example: If the date field is 08.10.04, I want the

Daysinmonth field
to show 31.

Thanks in advance!
Hallgeir


.

  #3  
Old September 21st, 2004, 04:30 PM
Allen Browne
external usenet poster
 
Posts: n/a
Default

The number of days in the month of the field TheDate is:
Day(DateSerial(Year([TheDate]), Month([TheDate]) + 1, 0))

The expression creates a date based on the month after TheDate (month + 1),
day zero. That's the day before the first, so the end of the month. The
Day() function then extracts that day.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Hallgeir" wrote in message
...
Is there a (easy) way to find the number of days in a month if I have the
date. Example: If the date field is 08.10.04, I want the Daysinmonth field
to show 31.

Thanks in advance!
Hallgeir



 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Formula for average units sold based on # days in month sold John Sebastian Worksheet Functions 1 December 29th, 2003 07:46 PM
Number of working days between two dates James Silverton Worksheet Functions 3 October 23rd, 2003 12:40 PM


All times are GMT +1. The time now is 06:07 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.