Hi, This is my first post. Let me with, I'm not a programmer I tinker, and figure things out.
I have a program I wrote that works in SmallBASIC (smallbasic dot github dot io) and a JPSoftware batch file (don't laugh until you try it). But for some reason it only partially works in QB64. If I run it for 2021-03-01 to 2021-10-19 it appears to be all right, I haven't run all of the dates, But if I run 2021-02-28 to 2021-10-19 it gives wrong data.
It should be:
233 Days,
or 0 years, 7 months, and 21 days,
or 7 months and 21 days,
or 33 weeks and 2 days.
In QB64 I'm getting:
600 Days,
or 0 years, 7 months, and 21 days,
or 7 months and 21 days,
or 85 weeks and 5 days.
Anyway here's the code.
[code=qb64]
Dim Shared y
Dim Shared m
Dim Shared d
Dim Shared JD
Dim Shared LrYr
Dim Shared LrMo
Dim Shared LrDa
Dim Shared SmYr
Dim Shared SmMo
Dim Shared SmDa
Dim Shared OpYr
Dim Shared OpMo
Dim Shared OpDa
Dim Shared DM
Dim Shared LY
Dim Shared dlm
Dim Shared dtm
Dim Shared dnm
' Get 1st Date.
Input " Enter first date (yyyy-mm-dd)"; Dt1$
Yr1 = Val(Left$(Dt1$, 4))
Mo1 = Val(Mid$(Dt1$, 6, 2))
Da1 = Val(Right$(Dt1$, 2))
y = Yr1
m = Mo1
d = Da1
Get_Date_to_JD
JD1 = JD
y = 0
m = 0
d = 0
' Get 2nd Date.
Input " Enter second date (yyyy-mm-dd)"; Dt2$
Yr2 = Val(Left$(Dt2$, 4))
Mo2 = Val(Mid$(Dt2$, 6, 2))
Da2 = Val(Right$(Dt2$, 2))
y = Yr2
m = Mo2
d = Da2
Get_Date_to_JD
JD2 = JD
' Determine which Date/JD is larger.
If JD1 > JD2 Then
LrJD = JD1
LrYr = Yr1
LrMo = Mo1
LrDa = Da1
LrDt$ = Dt1$
SmJD = JD2
SmYr = Yr2
SmMo = Mo2
SmDa = Da2
SmDt$ = Dt2$
Else
LrJD = JD2
LrYr = Yr2
LrMo = Mo2
LrDa = Da2
LrDt$ = Dt2$
SmJD = JD1
SmYr = Yr1
SmMo = Mo1
SmDa = Da1
SmDt$ = Dt1$
End If
' Find the difference between the larger and smaller date in days.
OpJD = LrJD - SmJD
' Convert of difference to years, months, and days.
Get_YMD
' Print the answer.
Print " JD1 = "; JD1; " º JD2 = "; JD2 ' This line is normally a blank print line.
' JD1 & JD2 are here for troubleshooting,
' because they don't agree my other 2 sources.
Print " The difference between "; SmDt$; " and "; LrDt$; " is:"
Print
Print " "; OpJD; " Days,"
Print " or "; OpYr; " years, "; OpMo; " months, and "; OpDa; " days,"
Print " or "; OpYr * 12 + OpMo; " months and "; OpDa; " days,"
Print " or "; Int(OpJD / 7); " weeks and "; OpJD Mod 7; " days."
' Done
End
' Find the Years, Months, and Days between the two dates.
Sub Get_YMD
' Find the days.
If SmDa = LrDa Then
OpDa = 0
Else
If SmDa < LrDa Then
OpDa = LrDa - SmDa
Else
DM = LrMo
y = LrYr
Get_Days_In_Month
OpDa = LrDa + dlm - SmDa
LrMo = LrMo - 1
End If
End If
' Find the months.
If SmMo = LrMo Then
OpMo = 0
Else
If SmMo < LrMo Then
OpMo = LrMo - SmMo
Else
OpMo = LrMo + 12 - SmMo
LrYr = LrYr - 1
End If
End If
' Find the years.
If LrYr = SmYr Then
OpYr = 0
Else
OpYr = LrYr - SmYr
End If
End Sub
' Get the number of days in a month for last month, this month, and next month.
Sub Get_Days_In_Month
Get_Leap_Year
DM = DM * 2 - 1
dlm = Val(Mid$("313128313031303131303130", DM, 2))
If dlm = 28 And LY = 1 Then dlm = 29
dtm = Val(Mid$("312831303130313130313031", DM, 2))
If dtm = 28 And LY = 1 Then dtm = 29
dnm = Val(Mid$("283130313031313031303131", DM, 2))
If dnm = 28 And LY = 1 Then dnm = 29
End Sub
' Check if a year is Leap Year.
Sub Get_Leap_Year
LY = 0
If y Mod 4 = 0 Then LY = 1
If y Mod 100 = 0 Then LY = 0
If y Mod 400 = 0 Then LY = 1
End Sub
' Get the value of the Julian Day for a Gregorian Date.
Sub Get_Date_to_JD
JD = d - 32075 + (1461 * (y + 4800 + ((m - 14) \ 12)) \ 4) + (367 * (m - 2 - ((m - 14) \ 12 * 12)) \ 12) - (3 * ((y + 4900 + ((m - 14) \ 12)) / 100) \ 4)
End Sub