I'm trying to create a function that calculates the number of months
between two dates. DateDiff won't work because I need to add one month
if the day of the end_date is greater than the day of the start_date.
I can't determine where the problem is in the code below. It returns 0
everytime. Any input is welcomed.
************************************
Public Function CalculateMonths(ipdtStart As Date, ipdtEnd As Date) As
Integer
Dim intNumberOfMonths As Integer
Dim intStartMonth As Integer
Dim intEndMonth As Integer
Dim intYears As Integer
intStartMonth = Month(Start_Date)
intEndMonth = Month(End_Date)
intYears = Year(End_Date) - Year(Start_Date)
If Day(ipdtStart) >= Day(ipdtEnd) Then
intNumberOfMonths = (intEndMonth - intStartMonth)
Else
intNumberOfMonths = (intEndMonth - intStartMonth) + 1
End If
CalculateMonths = intNumberOfMonths + (12 * intYears)
End Function 3 1182
<lp*******@aipt.org> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com... I'm trying to create a function that calculates the number of months between two dates. DateDiff won't work because I need to add one month if the day of the end_date is greater than the day of the start_date. I can't determine where the problem is in the code below. It returns 0 everytime. Any input is welcomed. ************************************
Public Function CalculateMonths(ipdtStart As Date, ipdtEnd As Date) As Integer
Dim intNumberOfMonths As Integer Dim intStartMonth As Integer Dim intEndMonth As Integer Dim intYears As Integer
intStartMonth = Month(Start_Date) intEndMonth = Month(End_Date) intYears = Year(End_Date) - Year(Start_Date)
If Day(ipdtStart) >= Day(ipdtEnd) Then intNumberOfMonths = (intEndMonth - intStartMonth) Else intNumberOfMonths = (intEndMonth - intStartMonth) + 1
End If
CalculateMonths = intNumberOfMonths + (12 * intYears)
End Function
How about something like:
Public Function CalculateMonths(ipdtStart As Date, ipdtEnd As Date) As
Integer
CalculateMonths = DateDiff("m", ipdtEnd, ipdtStart) _
- (DatePart("d", ipdtEnd) >= DatePart("d",
ipdtStart))
End Function
On 13 Jun 2005 08:15:20 -0700, lp*******@aipt.org wrote: I'm trying to create a function that calculates the number of months between two dates. DateDiff won't work because I need to add one month if the day of the end_date is greater than the day of the start_date. I can't determine where the problem is in the code below. It returns 0 everytime. Any input is welcomed.
What about an IFF? In general terms:
DateDiff + iff(Day(end_date)>Day(start_date),1,0)
Otherwise, to debug the following, put a STOP command at the
beginning. Once there, use F8 to step through the routine. Hover
over the variables to see their assigned values at any moment.
By the time you have walked through it once you will see what the
problem is.
mike
************************************
Public Function CalculateMonths(ipdtStart As Date, ipdtEnd As Date) As Integer
Dim intNumberOfMonths As Integer Dim intStartMonth As Integer Dim intEndMonth As Integer Dim intYears As Integer
intStartMonth = Month(Start_Date) intEndMonth = Month(End_Date) intYears = Year(End_Date) - Year(Start_Date)
If Day(ipdtStart) >= Day(ipdtEnd) Then intNumberOfMonths = (intEndMonth - intStartMonth) Else intNumberOfMonths = (intEndMonth - intStartMonth) + 1
End If
CalculateMonths = intNumberOfMonths + (12 * intYears)
End Function
On Mon, 13 Jun 2005 16:10:21 GMT, mb******@pacbell.net.invalid (Mike
Preston) wrote: On 13 Jun 2005 08:15:20 -0700, lp*******@aipt.org wrote:
I'm trying to create a function that calculates the number of months between two dates. DateDiff won't work because I need to add one month if the day of the end_date is greater than the day of the start_date. I can't determine where the problem is in the code below. It returns 0 everytime. Any input is welcomed.
What about an IFF? In general terms:
DateDiff + iff(Day(end_date)>Day(start_date),1,0)
Otherwise, to debug the following, put a STOP command at the beginning. Once there, use F8 to step through the routine. Hover over the variables to see their assigned values at any moment.
By the time you have walked through it once you will see what the problem is.
mike
************************************
Public Function CalculateMonths(ipdtStart As Date, ipdtEnd As Date) As Integer
Dim intNumberOfMonths As Integer Dim intStartMonth As Integer Dim intEndMonth As Integer Dim intYears As Integer
intStartMonth = Month(Start_Date) intEndMonth = Month(End_Date) intYears = Year(End_Date) - Year(Start_Date)
But it probably has something to do with the fact that the three lines
above reference "Start_Date" and "End_Date" when those variables
aren't anywhere to be found. You probably meant:
intStartMonth = Month(ipdtStart)
intEndMonth = Month(ipdtEnd)
intYears = Year(ipdtEnd) - Year(ipdtStart)
Although I didn't check the logic to see if it actually calculates the
interval you are looking for it to calculate.
mike
If Day(ipdtStart) >= Day(ipdtEnd) Then intNumberOfMonths = (intEndMonth - intStartMonth) Else intNumberOfMonths = (intEndMonth - intStartMonth) + 1
End If
CalculateMonths = intNumberOfMonths + (12 * intYears)
End Function This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chris Becker |
last post by:
I have the following query:
SELECT Month, Sum(Hits) AS Hits FROM tblHits GROUP BY Month ORDER BY
Month
Unfortunately it only returns rows for months that have data assigned
to them.
How...
|
by: manning_news |
last post by:
Is there an way to create a table of months using SQL functions? If
"July" is entered for the beginning month, and "December" is entered
for the ending month, then I'd like to create a table with...
|
by: Paolo |
last post by:
Friends I have a form whith three textboxes, A, B, C , D and F. The
first two are used to store a starting and ending date data. The C, D
and F should be a calculated form.
In textbox A I will...
|
by: Juan |
last post by:
Hi everyone,
is there a function that calculates the exact amount of Years, Months, and
Days between two days?
TimeDiff is not good enough, since it calculates, for example:
Date 1: Dec....
|
by: Patrick Hatcher |
last post by:
Is there a function that will give me the number of months, as an
integer, in Pg 7.4.x? I found the date_trunc function but that will
return text and I didn't see anything else?
I have this,...
|
by: Jose |
last post by:
Exists a function that determined between two dates the years,months and
days?
Thanks a lot.
|
by: RandyG |
last post by:
how do you pull the current months data without having to manually
input the date each time?
|
by: Steven W. Orr |
last post by:
I'm reading a logfile with a timestamp at the begging of each line, e.g.,
Mar 29 08:29:00
I want to call datetime.datetim() whose arg2 is a number between 1-12 so I
have to convert the month...
|
by: Bob Bedford |
last post by:
Hi all,
it's there any simple way to create an array of years and months dynamically
(2 loops) for an associative array ?
I've tried this so far: (having an error on last line with the "=>"...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |