Log In  


Hi,

Just wondered if anyone knows how I can calculate the number of days between a date assigned to a variable (or 3 variables actually) and the current date please?

I have googled around and now know that I can use the following to get the current date in Pico8:

year=stat(90)
month=stat(91)
day=stat(92)

And if I set up three variables to hold a date in the past e.g.

appyear=2018
appmonth=7
appday=13

How can I easily work out the number of days between the 2 dates please, or do I need to work up some code to acknowledge the different number of days in various months, and work it out like that? Sounds fiddly!!

Any help much appreciated :-)

Thank you!
Paul.



Thinking back on the old Tom Scott video about timezones, that kind of calculation can get extremely complicated depending on what kind of scope you're going for ... so that's probably your starting point: which dates do you want to be able to calculate days between?

For what it's worth, my gut instinct is, for dates A and B with A earlier than B:

  • Sum days in each year from year(A) to year(B)-1, accounting for leap years (and calendar changes if desired);
  • Calculate day within year for A and B, likewise accounting for;
  • return year sum minus day-within for A plus day-within for B - possibly subtracting one if you want to exclude the end date (i.e. to return 0 when starting and ending on the same day instead of 1)

Edit: Wait ... adding 1 if you want to include both ends? Check your results, I'm on my phone.


function daynumber(month,day,leap)
	local d=day+
		({0,31,59,90,120,151,181,212,243,273,304,334})[month]
		-- table corresponds to days to beginning of month
	if leap and month>2 then
		return d+1
	else
		return d
	end
end

function daysbetween(year0,month0,day0,year1,month1,day1)
	-- works for years between 1901 and 2099
	-- (i.e. leap year if year%4==0)

	-- years
	local d=365*(year1-year0)
	-- leap year correction
	-- year\4 increases with each leap year
	-- but leap year within given year accounted for
	d += (year1-1)\4-(year0-1)\4

	-- days in year correction
	d += daynumber(month1,day1,year1%4==0)
		- daynumber(month0,day0,year0%4==0)

	return d
end

...is 95 tokens - I've not done any optimization but it seems to work in some rough testing. Like the comments say, it only works for years between 1901 and 2099 because it doesn't account for Gregorian-calendar leap year math.


Actually, adding Gregorian calendar isn't too conceptually complicated - this unoptimized version is 143 tokens:

function daynumber(month,day,leap)
	local d=day+
		({0,31,59,90,120,151,181,212,243,273,304,334})[month]
		-- table corresponds to days to beginning of month
	if leap and month>2 then
		return d+1
	else
		return d
	end
end

function isleap(year)
	return year%4==0 and year%100~=0 or year%400==0
end

function daysbetween(year0,month0,day0,year1,month1,day1)
	-- gregorian calendar version
	-- years
	local d=365*(year1-year0)
	-- leap year correction
	-- year\4 increases with each leap year
	-- but leap year within given year accounted for
	d += (year1-1)\4-(year0-1)\4
	-- gregorian correction
	d -= (year1-1)\100-(year0-1)\100
	d += (year1-1)\400-(year0-1)\400

	-- days in year correction
	d += daynumber(month1,day1,isleap(year1))
		- daynumber(month0,day0,isleap(year0))

	return d
end


[Please log in to post a comment]