r/visualbasic • u/Brian-FL • Mar 14 '24
VB6 - calculate number of months between due date and date paid (including fractional parts of a month)
This is what the company uses so I have to use this old version, sorry.
If the solution is a ton of code, I do not want to waste anyones time so feel free to say "too much".
I have a simple version but it is wrong on 5 dates of the year.
PROBLEM:
DATEDIFF is an unrecognized function in our version, so I am stuck!!!!!!!!
I ONLY NEED HOW TO CALCULATE THE DIFFERENCE BETWEEN MONTHS PART (including fractional part).
Example:
Due Date - Date Paid = # of months (including fractional)
4/15/24 - 5/15/24 = 1 month
4/15/24 - 5/16/24 = 2 months (because it is 1 day past a month so the fraction is like 1.03 months, so since greater than 1, it become 2 months)
4/15/24 - 6/15/24 = 2 months
And so forth until they finally file their return and/or pay the tax due.
-----------------------------------------------------------------------------------------------------------------------------------------------
ACTUAL EXAMPLE IF YOU WANT:
You can skip this part below but it explains the exact situation and why I need the months to be correct for whenever the tax is paid:
This is for tax software, so I need to calculate a late filing penalty based on when they paid their tax due when filing past the due date.
The penalty is equal to 5% for each FRACTION of a month late (not a daily rate but rather a set rate for an entire 30-ish day period between months).
Example:
Tax Due = $20,000
Due Date = 4/15/2024
If they pay anytime between 4/16 - 5/15 the penalty will always be 5% (considered 1 month late).
The next months gets calcuated from 5/16 - 6/15 and will be another 5%.
So if they owe $20,000 at 5% for any part of the month the results would be:
Due date: 4/15/24
Pay 4/16/24 = $1,000 ($20,000 * 5% * 1 month)
Pay 5/15/24 = $1,000 (still within the 1 month late)
Pay 5/16/24 = $2,000 (because this is now a new month, even though just by 1 day)
Pay 6/15/24 = $2,000. (same concept, penalty is the same anytime paid before the next month starts)
Pay 6/16/24 = $3,000 (new month again.... 4/15 to 6/16 is 2 months and 1 day = 3 months)
Thank you for any advice provided.
3
u/geekywarrior Mar 14 '24
Are you sure you're in VB6? I just created a brand new VB6 project and DateDiff is there. The following code compiles and runs fine.
3
u/Fergus653 Mar 14 '24
If it gets too messy to do in VB6, you can write the tricky bits in a C# assembly and make a COM compatible interface, which you can call from VB6.
2
u/dan1101 Mar 14 '24
Datediff should be standard. Try this code in Immediate:
The first parameter is the interval, "m" = months and "d" = days. If it's less than 1 month it returns 0 for "m"
1
u/PunchyFinn Mar 15 '24
You wrote "a set rate for an entire 30-ish day period between months", but don't give the 'ish'es: leap year february? months with 31 days?
So I assumed 30 day period = month.
This takes less than 10 lines of code in 2 functions.
First function turns a date into a Julian Day, which has nothing to do with the Julian calendar. Totally different. Julian Day is used is astronomy and is a positive day count with day zero being near 5000 BC, give or take a few centuries.
Second Function uses first to determine the number of days between 2 dates and divides that total per set of 30 days. As is, these functions only work for between around 1600- 4000 AD, which for tax purposes hopefully isn't a problem for anyone.
--private Function CDateJulianDay(byval DayUsed as integer...and so forth...) as double
--Dim Base1 as integer
--Base1 = Int((14 - MonthUsed) / 12)
--CDateJulianDay = DayUsed + Int(367 * (MonthUsed + (Base1 * 12) - 2) / 12) + Int(1461 * (YearUsed + 4800 - Base1) / 4) - 32113
-- 'julian day to gregorian date adjustment, julian day at noon
--CDateJulianDay = CDateJulianDay - (Int(3 * Int((YearUsed + 100 - Base1) / 100) / 4) - 2)
--End Function
you can use a date class/type for the 2 dates in the argument, but you still have to split them up into the separate parts so I'm using the split up parts in the argument.
--Private Function DateDifferenceMonths(byval Day1Used as integer ...Day2Used..., byref ReturnedRemainder as integer) as integer
'Second Date is the expected more recent/older date, 'returns whole 30 day periods if months returned is less than zero, not bothering counting remainder days
--Dim JulianDay1 as double
--dim JulianDay2 as double
--JulianDay1= CDateJulianDay(day1used,month1used,year1used)
--JulianDay2= CDateJulianDay(day2used,month2used,year2used)
--if julianday2 <=JulianDay1 then exit function
-- DateDifferenceMonths= int( (JulianDay2-JulianDay1)/30)
--ReturnedRemainder = (JulianDay2-JulianDay1) - ( DateDifferenceMonths * 30)
--end function
I hope this helps. The Julian Day function makes the use of a vba date unnecessary. A slightly larger function lets the julian day work in the BC era and at least one major database uses the Julian Day for dates. The Windows calculator has a date difference option. It cannot handle dates before the year 1601 or dates after the year 9999.
2
u/jd31068 Mar 15 '24
It is in VB4 as well https://imgur.com/a/y5VGAEl