Days to Months and Years

How to convert days to months and years
So if days = 7 it’s only gonna print (“7 Days”)
but if it’s like days = 37 it’s gonna print (“1 Month 7 Days”), and same to Year.

1 Like

Assuming months = 30 days and years = 12 months, you can do:

local days = 37
local months = math.floor(days / 30)
days = days % 30
local years = math.floor(months / 12)
months = months % 12

You can add the according conditions to check if any of the variable’s value is 0, filtering it on the output.

5 Likes
local function dayCountConverter(n)
	local years = math.floor(n / 365)
	local months = math.floor((n - (years * 365)) / 30)
	local days = n - (years * 365) - (months * 30)
	return string.format("%i days is %i years, %i months and %i days.", n, years, months, days)
end

print(dayCountConverter(400)) --400 days is 1 years, 1 months and 5 days.
1 Like