QB64 Phoenix Edition
Date functions - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11)
+--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2)
+--- Thread: Date functions (/showthread.php?tid=2481)

Pages: 1 2 3 4


RE: Date functions - Kernelpanic - 03-08-2024

(03-08-2024, 05:01 PM)mdijkens Wrote: I think all are good, just depends on need and style  Smile
Yes, thanks! Today it's just a hobby for me to distract myself.
I loved programming and would like to do it as much as I used to, but times have changed.

C'est la vie, as the Frenchman says!


RE: Date functions - dano - 05-20-2024

For date difference calculations why not convert to Julian and get the difference?


Code: (Select All)
Start:
Input "Date1: ", d1$
If d1$ = "" Then System
Input "Date2: ", d2$
Print "Difference in days: "; Julian#(d2$) - Julian#(d1$)
Print: Print: Print
GoTo Start



Function Julian# (from$)
    'Gives # of days since Monday, January 1, 4713 BC
    'accepts xx-xx-xxxx OR xx/xx/xxxx OR xx-xx-xx OR xx/xx/xx

    'from$ must be either 8 or 10 characters
    If Len(from$) <> 8 And Len(from$) <> 10 Then Julian# = 0: Exit Function

    'must be xx-xx or xx/xx format
    If Mid$(from$, 3, 1) <> "-" And Mid$(from$, 3, 1) <> "/" Then Julian# = 0: Exit Function
    If Mid$(from$, 6, 1) <> "-" And Mid$(from$, 6, 1) <> "/" Then Julian# = 0: Exit Function

    Month% = Val(Mid$(from$, 1, 2))
    Day% = Val(Mid$(from$, 4, 2))
    If Len(from$) = 10 Then
        Year% = Val(Right$(from$, 4))
    ElseIf Len(from$) = 8 Then
        Year% = Val("20" + Right$(from$, 2))
    End If

    Select Case Month%
        Case Is >= 3
            Month% = Month% - 3
        Case Is <= 2
            Month% = Month% + 9
            Year% = Year% - 1
    End Select

    A# = 146097# * Fix(Fix(Year% / 100) / 4)
    B# = Fix(1461# * (Year% Mod 100) / 4)
    C# = Fix((153# * Month% + 2) / 5) + Day% + 1721119

    Julian# = A# + B# + C#
End Function