Search code examples
lua

How to add days to a date generated by os.date() in Lua


There is a lua function called date() in the os module. Calling os.date() will give the current date and time in the format: Tue Aug 10 13:04:17 2021.

Doing it like: os.date("%x") gives us the following: 08/10/21.

Is it possible to manipulate the date function to add days to the current datetime/date returned? Something like what happens when you try to add days to a JavaScript date using .setDate() mutator. I've already had a look at How to add days in given datetime in Lua but it's not what I want to achieve.

Thanks for your assistance in advance.


Solution

  • Use os.time and compute with seconds:

    t=os.time()
    print(os.date("%c",t))
    d=12
    t=t+d*24*60*60
    print(os.date("%c",t))