This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

lua table range of dates
#3
(20.08.2020, 06:46)admin Wrote: You need to use modulo (%) operator for this:
Code:
data = {}

today = os.date('%w')
for i = 1, 14 do
  data[ i ] = (today - i) % 7
end

log(data)
Many thanks,
how can i also get the associated date for the previous days?, taking into account if the range of 14 days is between 2 months,
i have so far the below it assumes feb has 28 days but im not sure it is correct and works properly....
also, is there a way to include the leap year so days_in_month {Feburary} is changing depending if it is 28 or 29 days?

Code:
days_in_month = {31, 28, 31, 30 ,31, 30,  31, 31, 30, 31, 30, 31}

data = {}

today = os.date('%w') -- week day number
month_day =  tonumber(os.date('%d') )-- date in month for day
month_num = tonumber(os.date('%m'))  --%m month (09) [01-12]

for i = 1, 60 do

  mth_day  = (month_day - i) % days_in_month[month_num]
 
  if mth_day < 1 then
    log('exceeded month getting prev month date')
    month_num = month_num-1
    mth_day = days_in_month[month_num]
  end
     
  data[ i ] = { day_num = (today -i) %7, day_in_month = mth_day, month_date = month_num}
   
end
log(data)
Reply


Messages In This Thread
lua table range of dates - by benanderson_475 - 20.08.2020, 05:17
RE: lua table range of dates - by admin - 20.08.2020, 06:46
RE: lua table range of dates - by benanderson_475 - 20.08.2020, 23:12
RE: lua table range of dates - by Dré - 20.06.2021, 11:48
RE: lua table range of dates - by admin - 20.06.2021, 12:25

Forum Jump: