473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

What's wrong with this get last day of month function?

MLH
Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date
'************************************************* *************************
' Accepts a date. Determines month & year of the date. Returns
' the date of the last day of that month (in the same year)
'************************************************* *************************
GetLastDayOfMonth = DateSerial(Year(dtDay), Month(dtDay), 31)

End Function

If I enter dtDay value of 6/15/2005, the FN returns 7/1/2005 instead
of 6/30/2005. What's wrong?
Nov 13 '05 #1
7 6011
MLH wrote:
Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date
'************************************************* *************************
' Accepts a date. Determines month & year of the date. Returns
' the date of the last day of that month (in the same year)
'************************************************* *************************
GetLastDayOfMonth = DateSerial(Year(dtDay), Month(dtDay), 31)

End Function

If I enter dtDay value of 6/15/2005, the FN returns 7/1/2005 instead
of 6/30/2005. What's wrong?


Not all months have 31 days. Use...

GetLastDayOfMonth = DateSerial(Year(dtDay), Month(dtDay) + 1, 0)

DateSerial is smart enough to know that the zeroth of a month is equal to the
last day of the previous month regardless of how many days are in that month.

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 13 '05 #2
MLH
That's pretty sneaky, Rick. I like it. Thx a bunch.
GetLastDayOfMonth = DateSerial(Year(dtDay), Month(dtDay) + 1, 0)


Nov 13 '05 #3
On Sun, 10 Jul 2005 19:23:00 GMT, "Rick Brandt" <ri*********@hotmail.com>
wrote:
MLH wrote:
Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date
'************************************************* *************************
' Accepts a date. Determines month & year of the date. Returns
' the date of the last day of that month (in the same year)
'************************************************* *************************
GetLastDayOfMonth = DateSerial(Year(dtDay), Month(dtDay), 31)

End Function

If I enter dtDay value of 6/15/2005, the FN returns 7/1/2005 instead
of 6/30/2005. What's wrong?


Not all months have 31 days. Use...

GetLastDayOfMonth = DateSerial(Year(dtDay), Month(dtDay) + 1, 0)

DateSerial is smart enough to know that the zeroth of a month is equal to the
last day of the previous month regardless of how many days are in that month.


That works, but it's very confusing to understand how. Here's another method
that takes more code, but I like it better anyway...

Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date

Dim dtFirstOfMonth As Date
dtFirstOfMonth = DateAdd("d", 1 - Day(tdDay), tdDay)

Dim dtFirstOfNextMonth As Date
dtFirstOfNextMonth = DateAdd("m", 1, dtFirstOfMonth)

GetLastDayOfMonth = DateAdd("d", -1, dtFirstOfNextMonth)
End Function

Nov 13 '05 #4
"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:n5********************************@4ax.com...
On Sun, 10 Jul 2005 19:23:00 GMT, "Rick Brandt" <ri*********@hotmail.com>
wrote:
MLH wrote:
Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date
'************************************************* *************************
' Accepts a date. Determines month & year of the date. Returns
' the date of the last day of that month (in the same year)
'************************************************* *************************
GetLastDayOfMonth = DateSerial(Year(dtDay), Month(dtDay), 31)

End Function

If I enter dtDay value of 6/15/2005, the FN returns 7/1/2005 instead
of 6/30/2005. What's wrong?


Not all months have 31 days. Use...

GetLastDayOfMonth = DateSerial(Year(dtDay), Month(dtDay) + 1, 0)

DateSerial is smart enough to know that the zeroth of a month is equal to
the
last day of the previous month regardless of how many days are in that
month.


That works, but it's very confusing to understand how. Here's another
method
that takes more code, but I like it better anyway...

Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date

Dim dtFirstOfMonth As Date
dtFirstOfMonth = DateAdd("d", 1 - Day(tdDay), tdDay)

Dim dtFirstOfNextMonth As Date
dtFirstOfNextMonth = DateAdd("m", 1, dtFirstOfMonth)

GetLastDayOfMonth = DateAdd("d", -1, dtFirstOfNextMonth)
End Function


If it's wrapped in a function, I don't see what it matters if not everyone
understands how it works. The name of the function makes it obvious what
it's supposed to be doing.

Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date
GetLastDayOfMonth = DateSerial(Year(dtDay), Month(dtDay) + 1, 0)
End Function
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

Nov 13 '05 #5
Br
Steve Jorgensen <no****@nospam.nospam> wrote:
On Sun, 10 Jul 2005 19:23:00 GMT, "Rick Brandt"
<ri*********@hotmail.com> wrote:
MLH wrote:
Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date
'************************************************* *************************
' Accepts a date. Determines month & year of the date. Returns
' the date of the last day of that month (in the same year)
'************************************************* *************************
GetLastDayOfMonth = DateSerial(Year(dtDay), Month(dtDay), 31)

End Function

If I enter dtDay value of 6/15/2005, the FN returns 7/1/2005 instead
of 6/30/2005. What's wrong?


Not all months have 31 days. Use...

GetLastDayOfMonth = DateSerial(Year(dtDay), Month(dtDay) + 1, 0)

DateSerial is smart enough to know that the zeroth of a month is
equal to the last day of the previous month regardless of how many
days are in that month.


That works, but it's very confusing to understand how. Here's
another method that takes more code, but I like it better anyway...

Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date

Dim dtFirstOfMonth As Date
dtFirstOfMonth = DateAdd("d", 1 - Day(tdDay), tdDay)

Dim dtFirstOfNextMonth As Date
dtFirstOfNextMonth = DateAdd("m", 1, dtFirstOfMonth)

GetLastDayOfMonth = DateAdd("d", -1, dtFirstOfNextMonth)
End Function


I use.....

LastDayOfMonth = DateAdd("d", -1, DateSerial(Year(myDate), Month(myDate)
+ 1, 1))
--
regards,

Bradley

A Christian Response
http://www.pastornet.net.au/response
Nov 13 '05 #6
Br@dley wrote:
I use.....

LastDayOfMonth = DateAdd("d", -1, DateSerial(Year(myDate),
Month(myDate) + 1, 1))


But why have the overhead of running two functions when DateSerial is perfectly
capable of doing this by itself?

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com


Nov 13 '05 #7
Br
Rick Brandt <ri*********@hotmail.com> wrote:
Br@dley wrote:
I use.....

LastDayOfMonth = DateAdd("d", -1, DateSerial(Year(myDate),
Month(myDate) + 1, 1))


But why have the overhead of running two functions when DateSerial is
perfectly capable of doing this by itself?


I was just saying that _was_ how I did it compared to the several lines
of code used in the previous post. Obviously now I know zero has the
same effect I'll use that.
--
regards,

Bradley

A Christian Response
http://www.pastornet.net.au/response
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: sugaray | last post by:
hi, below is my program for doing exercise with library time functions, something is not right with it, and for the time being i couldn't figure out what's wrong and where, thanx for your help. ...
12
by: questions? | last post by:
I am testing a problem with linked list. I just do a lot of times: create a list, then free it. ############################################# # include <stdio.h> # include <stdlib.h> struct...
0
by: Benny Ng | last post by:
Dear All, Modify1("SqlServer", "xxxxxxxxxxxxxx"); The error message is program can't find the XML segment <add name="SqlServer" , But it seems should be runs properly. What's wrong with the...
2
by: Paulo da Silva | last post by:
Hi! What's wrong with this way of subclassing? from datetime import date class MyDate(date): def __init__(self,year,month=None,day=None): if type(year) is str: # The whole date is here as...
0
by: anniebai | last post by:
Please help me with this RowUpdating function. I copied it from the internet and modified the part that I could understand. and not surprisingly, it's not working. When I tried to update a row,...
7
by: fl | last post by:
Hi, I am learning C++ with C++ primer written by Lippman. The first output line: cout << s1 << endl; works right. The second is wrong. I find the problem is that s1 is destroyed in the call from...
3
by: yaoc1987 | last post by:
hello everyone im writing a program for "sequence class with dynamic arrays" every time i compile it gives me this error: i believe all 3 is the same to use copy i know i have to include...
1
by: yaoc1987 | last post by:
my assignment was to implement a sequence class with dynamic array for some reason the only error is this: 1>c:\documents and settings\joe\desktop\spring 2010\csc 212\hw\hw #3\sequence2.cxx(53) :...
2
by: flower88 | last post by:
Hello, I'm trying to get rid of duplicate entries for contacts. So in my contact entry form when the save buttom is clicked I want to use the DCount function to see if there are duplicates. Here...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.