Chaos Posted May 10, 2016 Share Posted May 10, 2016 hi, i have a question how do i get max days of a month like this month has 31 max day ? Link to comment
Noki Posted May 15, 2016 Share Posted May 15, 2016 I don't believe there is a function that specifically gives you the number of months in a year. So we usually use a table to act as a database of sorts. However, we have to remember that there are leap years and that February has 29 days instead of the usual 28 on these years. local months = { [1] = 31, [2] = 28, [3] = 31, [4] = 30, [5] = 31, [6] = 30, [7] = 31, [8] = 31, [9] = 30, [10] = 31, [11] = 30, [12] = 31, } This StackOverflow answer gives us some pseudo-code to calculate a leap year. Let's convert that to Lua. function isLeapYear(year) if ((year % 4 == 0) and (year % 100 ~= 0)) or (year % 400 == 0) then return true end return false end function getMonthMaxDays(month, year) if (months[month]) then if (month ~= 2 and not isLeapYear(year)) then return months[month] else return 29 end end return false end getMonthMaxDays(2, 2016) --> 29 getMonthMaxDays(2, 2011) --> 28 getMonthMaxDays(2, 2000) --> 29 getMonthMaxDays(11, 1945) --> 30 I also recommend using getRealTime.year to get the years since 1900. Just add 1900 onto that result to get the current year. That way the code will work years into the future! Note: This is all untested. Edit: Changed a few words and a couple of typos. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now