# DATE CALCULATIONS

``` BBS: Inland Empire Archive
Date: 06-20-92 (17:30)             Number: 1042
From: DOUGLAS LUSHER               Refer#: NONE
To: DOUGLAS LUSHER                Recvd: NO
Subj: DATE CALCULATIONS              Conf: (2) Quik_Bas```
``` Here are the promised date manipulation functions:
DIM Days%(12)
Days%(1) = 0: Days%(2) = 31: Days%(3) = 59: Days%(4) = 90
Days%(5) = 120: Days%(6) = 151: Days%(7) = 181: Days%(8) = 212
Days%(9) = 243: Days%(10) = 273: Days%(11) = 304: Days%(12) = 334

FUNCTION GregDate\$ (Julian&) STATIC
'returns the date of the Gregorian calendar for the supplied
'Julian day number, returns a string formated "MM-DD-YYYY"
STATIC Days&, Month%, Day%, Year%, DateString\$

Days& = Julian& - 1721425
Year% = (Days& \ 146097) * 400
Days& = Days& MOD 146097
'the value 146,097 is the number of days in four hundred years
'given by ((365 * 4 + 1) * 25 - 1) * 4 + 1
Year% = Year% + (Days& \ 36524) * 100
Days& = Days& MOD 36524
'the value 36,524 is the number of days in a century
'given by (365 * 4 + 1) * 25 - 1
Year% = Year% + (Days& \ 1461) * 4
Day% = Days& MOD 1461
'the value 1,461 is the number of days in four years, 365 * 4 + 1
IF Day% = 0 THEN
Day% = 366
ELSE
Year% = Year% + Day% \ 365
Day% = Day% MOD 365
IF Day% = 0 THEN
Day% = 365
ELSE
Year% = Year% + 1
END IF
END IF
IF LeapYear(Year%) THEN
SELECT CASE Day%
CASE IS <= 31: Month% = 1
CASE IS <= 60: Month% = 2: Day% = Day% - 31
CASE IS <= 91: Month% = 3: Day% = Day% - 60
CASE IS <= 121: Month% = 4: Day% = Day% - 91
CASE IS <= 152: Month% = 5: Day% = Day% - 121
CASE IS <= 183: Month% = 6: Day% = Day% - 152
CASE IS <= 214: Month% = 7: Day% = Day% - 182
CASE IS <= 244: Month% = 8: Day% = Day% - 213
CASE IS <= 274: Month% = 9: Day% = Day% - 244
CASE IS <= 305: Month% = 10: Day% = Day% - 274
CASE IS <= 335: Month% = 11: Day% = Day% - 305
CASE ELSE: Month% = 12: Day% = Day% - 335
END SELECT
ELSE 'IF NOT LeapYear(Year%) THEN
SELECT CASE Day%
CASE IS <= 31: Month% = 1
CASE IS <= 59: Month% = 2: Day% = Day% - 31
CASE IS <= 90: Month% = 3: Day% = Day% - 59
CASE IS <= 120: Month% = 4: Day% = Day% - 90
CASE IS <= 151: Month% = 5: Day% = Day% - 120
CASE IS <= 181: Month% = 6: Day% = Day% - 151
CASE IS <= 212: Month% = 7: Day% = Day% - 181
CASE IS <= 243: Month% = 8: Day% = Day% - 212
CASE IS <= 273: Month% = 9: Day% = Day% - 243
CASE IS <= 304: Month% = 10: Day% = Day% - 273
CASE IS <= 334: Month% = 11: Day% = Day% - 304
CASE ELSE: Month% = 12: Day% = Day% - 334
END SELECT
END IF

DateString\$ = "00-00-0000"
MID\$(DateString\$, 1, 2) = RIGHT\$(STR\$(Month% + 100), 2)
MID\$(DateString\$, 4, 2) = RIGHT\$(STR\$(Day% + 100), 2)
MID\$(DateString\$, 6, 5) = STR\$(-Year%)
GregDate\$ = DateString\$
END FUNCTION

FUNCTION JuliDate& (DateString\$) STATIC
'returns the Julian day number for the date string supplied
'in the format "MM-DD-YYYY"
STATIC Month%, Day%, Year%, Temp&
SHARED Days%()

Month% = VAL(LEFT\$(DateString\$, 2))
Day% = VAL(MID\$(DateString\$, 4, 2))
Year% = VAL(RIGHT\$(DateString\$, 4)) - 1

'calculate the number of days in all preceeding years
Temp& = (Year% * 365&) + (Year% \ 4) - ((Year% \ 100) - (Year% \ 400))

'add the number of days in the year to the start of the specified month
'if it's a leap year and after February, add one more day
Temp& = Temp& + Days%(Month%) - (LeapYear%(Year% + 1) AND (Month% > 2))

'add the number of days in the specified month
JuliDate& = Temp& + Day% + 1721425
END FUNCTION
'====== continued next message ======

--- TMail v1.29
* Origin: TC-AMS MLTBBS 2.2 - Minnetonka, MN (612)-938-4799 (1:282/7)
``` Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET