FANDOM


In this tutorial, I'm going to show you how to compute the difference in days between two dates. Let's say for simplicity's sake that you have stored an old date in a MEM$ file by just storing the DATE$ variable. Simple enough, that's just:

MEM$=DATE$
SAVE "MEM:WHATEVER"

OK, that's our old date. Let's say we come back some days later and want to compute the number of days that have passed since that stored date. Well, we'd first load the date:

LOAD "MEM:WHATEVER"
OLDDATE$=MEM$

Now comes the important part: we have an OLDDATE$ string which may be something like 2013/05/28, and now our current DATE$ is something like 2013/11/8. There's a nifty function called DTREAD which will extract the year, month, and day for us. Nifty!

DTREAD(OLDDATE$), OLDYEAR, OLDMONTH, OLDDAY
DTREAD(DATE$), YEAR, MONTH, DAY

Now we need to somehow use this information to compute the difference in days. You can try to ponder how you can somehow take into account the number of days in a month and the complicated "leap year" system (where we gain an extra day every 4 years, but not every 100, but every 400th we do again), but fortunately we have the internet, and we already have a function for that:

m = (m + 9) % 12
y = y - m/10
nd = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 )

Where m is the month, y is the year, d is the day, and nd will be the number of days since year 0. You can try to wrap your mind around how this works, but I'm not going to explain it (that's not really the purpose of this tutorial). Computing the days since year 0 may seem useless; didn't we want to compute the difference between two of our dates, not the difference between one date and year 0? Ah, but ponder this: If we compute the number of days since year 0 for both the old date and the new date... couldn't we just subtract those two numbers to get the difference? Yes, of course we can. Think of it this way, instead of doing "3 - 1" days, we're doing "100003 - 100001" days, which still gives us the same answer.

I'm going to alter the function a bit to suit Petit Computer's needs. First, instead of computing the day difference between a date and year 0, we're going to compute the difference between a date and year 2000. This significantly reduces the number, and won't cause a silly "OVERFLOW" error on Petit Computer. I'm also going to have to use FLOOR on the division, because the function given uses integer division but Petit Computer uses regular division (FLOOR will basically convert regular division into integer division). If you're confused, don't worry. Let's just see how to do it then, shall we? Remember, we've got OLDYEAR, OLDMONTH, and OLDDAY as our previous date, and YEAR, MONTH, DAY as our new date. To compute the difference in days using the algorithm above:

'Compute days since the year 2000 for the old date
OLDMONTH=(OLDMONTH+9)%12
OLDYEAR=OLDYEAR-2000-(OLDMONTH>=10)
OLDDAYNUM=365*OLDYEAR + FLOOR(OLDYEAR/4) - FLOOR(OLDYEAR/100) + FLOOR((OLDMONTH*306 + 5)/10) + ( OLDDAY - 1 )

'Compute days since the year 2000 for the current date
MONTH=(MONTH+9)%12
YEAR=YEAR-2000-(MONTH>=10)
DAYNUM=365*YEAR + FLOOR(YEAR/4) - FLOOR(YEAR/100) + FLOOR((MONTH*306 + 5)/10) + ( DAY - 1 )

'Finally, just get the day difference to know how many days have passed
DAYSPASSED=DAYNUM-OLDDAYNUM

And that's it! DAYSPASSED will now hold the number of days that have passed between our two dates. If you need extra help (of if something seems off), please let me know!

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.