Logic Machine Forum
lua table range of dates - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: lua table range of dates (/showthread.php?tid=2799)



lua table range of dates - benanderson_475 - 20.08.2020

How can i create a lua table of the last dates and days for the previous 14 day period, starting at today?  
I want to end up with {day 1 = {date = "8/19/2020", day = "wednesday"}, day_two = {date = "7/19/2020", day = "Tuesday"}....... }

i started with 
Code:
t = os.date("*t")
data ={}
today = os.date('%w') --%w weekday (3) [0-6 = Sunday-Saturday]
Index  = 1

for i = 1, 14 do

data[i] = today -Index
Index = Index+1

end
how can i filter out the -ve values and make it circular 0-6  to line up with the %w pattern


RE: lua table range of dates - admin - 20.08.2020

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)



RE: lua table range of dates - benanderson_475 - 20.08.2020

(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)



RE: lua table range of date - Erwin van der Zwart - 21.08.2020

Hi,

You made it way to complicated (: Try this:
Code:
time = os.time()

data = {}

for i = 1, 365 do
  datetimetable = os.date('*t', time)
  data[ i ] = { day_num = datetimetable.wday, week_day = os.date('%A', time), day_in_month = datetimetable.day, month_date = datetimetable.month}
  time = time - 86400 -- substract 1 day
end

log(data)
To have a table like in your initial question try this:
Code:
time = os.time()

data = {}

for i = 1, 14 do
  data['day ' .. i] = { date =  os.date('%x', time), day = os.date('%A', time)}
  time = time - 86400 -- substract 1 day
end

log(data)
BR,

Erwin


RE: lua table range of date - benanderson_475 - 22.08.2020

(21.08.2020, 07:21)Erwin van der Zwart Wrote: Hi,

You made it way to complicated (: Try this:
Code:
time = os.time()

data = {}

for i = 1, 365 do
  datetimetable = os.date('*t', time)
  data[ i ] = { day_num = datetimetable.wday, week_day = os.date('%A', time), day_in_month = datetimetable.day, month_date = datetimetable.month}
  time = time - 86400 -- substract 1 day
end

log(data)
To have a table like in your initial question try this:
Code:
time = os.time()

data = {}

for i = 1, 14 do
  data['day ' .. i] = { date =  os.date('%x', time), day = os.date('%A', time)}
  time = time - 86400 -- substract 1 day
end

log(data)
BR,

Erwin
Hi Erwin,

Yes i did make it very complicated..., your way is much better 

Many Thanks


RE: lua table range of dates - Dré - 20.06.2021

(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)



please can you tell me where i can find that module to import in the LM?


RE: lua table range of dates - admin - 20.06.2021

It's a built-in arithmetic operator: https://www.lua.org/manual/5.1/manual.html#2.5.1