Working with dates

I am trying to make a list of dates, starting from today.

The start is easy, I use the commands

 local date = os.date( “*t” ) 

  print( date.year, date.month, date.day )  

  print(date.wday)

Now I would like to make a loop that increments the day with one and adds the new day to a table

My table WeekDays has the local strings of the weekdays and MonthNames the local strings for months

for i = 0,90 do date.day = date.day +1 local weekday = tonumber(os.date("%w", os.time(date))) if weekday then print("weekday: "..weekday) print("weekday: ".. WeekDays[weekday+1]) end days[i] = WeekDays[weekday+1].." "..date.day.." "..MonthNames[date.month]..date.year end

The name of the Weekdays is incremented and shows “Monday”, “Tuesday” etc in the local language.

And the days keeps increasing: 1,2,3,4… and then it goes on to 31,32,33…

I would have liked the date to be added so that the month and year changes automatically, like the dateadd() function in other languages so that the date increases from 31. Januar 2018 to 1. February 2018 when I add date.day = date.day +1 

The best solution I have found was on this page, but then I need to also consider leap-years. 

https://www.promixis.com/forums/archive/index.php/t-8653.html

It could be done, but is there really no easy way to increment the date value, so that I can add it to a picker wheel ?

Honestly I would with os.time() and for each new day add 86400 (number of seconds in a day) then use os.date() to convert that value to whatever string values you need.  You can always take your table of dates like you’re using and pass it to os.time() to convert it to an integer value in seconds too.

Rob

Thank you, but I still do not know how to get the incremented data back into a variable. Can you give me an example ?

I tried 

local date = os.date( “*t” ) 

date.sec = date.sec+86400 

but something like

local nextdate = os.date(date)

only gives me an error. 

local now = os.time() for i = 1, 10 do      now = now + 86000      local date = os.date( "\*t", now )      -- do with the date table what you need end

something like that.

Rob

Thank you, that worked fine

Here is the code as it looks now, in case someone needs it.

 local days = {} local now = os.time() now = now - 86000 for i = 1, 100 do now = now + 86000 local date = os.date( "\*t", now ) local weekday = date.wday days[i] = WeekDays[weekday].." "..date.day.." "..MonthNames[date.month].." "..date.year end

It sounds like you might find luatz interesting.

Thank you, that looks very interesting.

Honestly I would with os.time() and for each new day add 86400 (number of seconds in a day) then use os.date() to convert that value to whatever string values you need.  You can always take your table of dates like you’re using and pass it to os.time() to convert it to an integer value in seconds too.

Rob

Thank you, but I still do not know how to get the incremented data back into a variable. Can you give me an example ?

I tried 

local date = os.date( “*t” ) 

date.sec = date.sec+86400 

but something like

local nextdate = os.date(date)

only gives me an error. 

local now = os.time() for i = 1, 10 do      now = now + 86000      local date = os.date( "\*t", now )      -- do with the date table what you need end

something like that.

Rob

Thank you, that worked fine

Here is the code as it looks now, in case someone needs it.

 local days = {} local now = os.time() now = now - 86000 for i = 1, 100 do now = now + 86000 local date = os.date( "\*t", now ) local weekday = date.wday days[i] = WeekDays[weekday].." "..date.day.." "..MonthNames[date.month].." "..date.year end

It sounds like you might find luatz interesting.

Thank you, that looks very interesting.