Author Topic: problem with Yr-Mo-Da difference program  (Read 3988 times)

0 Members and 1 Guest are viewing this topic.

Offline redwdc

  • Newbie
  • Posts: 4
    • View Profile
problem with Yr-Mo-Da difference program
« on: October 19, 2021, 05:13:04 pm »
Code: [Select]
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
« Last Edit: October 19, 2021, 06:19:35 pm by redwdc »

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: problem with Yr-Mo-Da difference program
« Reply #1 on: October 19, 2021, 05:33:19 pm »
Windows calculator gives the difference between Feb 28 2021 and October 19 2021 as 7 months, 3 weeks or 233 days

Offline redwdc

  • Newbie
  • Posts: 4
    • View Profile
Re: problem with Yr-Mo-Da difference program
« Reply #2 on: October 19, 2021, 05:58:13 pm »
Thanks Jack. I fixed it.
I must have copied the wrong text form the screen. I had several of the up there.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: problem with Yr-Mo-Da difference program
« Reply #3 on: October 19, 2021, 06:02:34 pm »
you need to edit your first post, you forgot a [ before code=qb64 so the whole post is off
when you omitted [ and then ended with /code the forum inserted [ code ] at the beginning of your post, so you need to delete it and insert [ at the proper place
« Last Edit: October 19, 2021, 06:32:38 pm by jack »

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: problem with Yr-Mo-Da difference program
« Reply #4 on: October 19, 2021, 07:13:08 pm »
@redwdc
your Sub Get_Date_to_JD is faulty
try
Code: QB64: [Select]
  1. Sub Get_Date_to_JD
  2.     Temp = (m - 14) \ 12
  3.     JD = d - 32075 + (1461 * (y + 4800 + Temp) \ 4) + (367 * (m - 2 - Temp * 12) \ 12) - (3 * ((y + 4900 + Temp) \ 100) \ 4)
  4.  

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: problem with Yr-Mo-Da difference program
« Reply #5 on: October 19, 2021, 08:27:08 pm »
Makes me want to dig up my old calendar program.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline redwdc

  • Newbie
  • Posts: 4
    • View Profile
Re: problem with Yr-Mo-Da difference program
« Reply #6 on: October 20, 2021, 04:17:36 pm »
@jack Thank you that's fixed it.

Strange though the original formula works in both of the other programs.  I found it on a US Navy site so I kind thought it should be good across the board.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: problem with Yr-Mo-Da difference program
« Reply #7 on: October 20, 2021, 04:33:52 pm »
@jack Thank you that's fixed it.

Strange though the original formula works in both of the other programs.  I found it on a US Navy site so I kind thought it should be good across the board.

Where's @TerryRitchie when we need him? I mean when you really need help, screw the Navy, send in the Marines!

Pete ⛵🔫
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/