This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
133spider's profile picture

Day Formatter

Started by 133spider, 21 February 2015 - 11:59 PM
133spider #1
Posted 22 February 2015 - 12:59 AM
Coded this in a hurry, so don't expect much from it. (Meaning I didn't have enough time to add leap years in the calculation)
Also, it would be nice if you were to give me credit if you're going to use this piece of code because it took me awhile to make this calculation.

Spoiler

format_day = function()
	days_in_months = {31,28,31,30,31,30,31,31,30,31,30,31}
	months = {31,59,90,120,151,181,212,243,273,304,334,365}
	sMonths = {"January","February","March","April","May","June","July","August","September","October","November","December"}
	_day = os.day()
	_y = math.floor(_day/365)
	d = _day - (365*_y)
	if _day % 365 == 0 then _y = _y - 1 end
	for i,v in pairs(months) do
		if (d - v) < 0 then
			_m = i
			_d = days_in_months[i] - math.abs(d - v)
			if _d == 0 then
				if _m == 1 then _m = 12 else _m = _m - 1 end
				_d = days_in_months[_m]
			end
			break
		end
	end		
	_m = sMonths[_m]
	return _m.." ".._d..", ".._y
end

Dog #2
Posted 22 February 2015 - 01:18 AM
Nicely done. I have a project I might use this in (with proper credit, of course). Thanks for sharing this :)/>