This topic contains 2 replies, has 2 voices, and was last updated by  Sandeep 1 month, 3 weeks ago.

Get previous day's date in LUA

  • What is the best way to get the previous day’s date in Lua? I tried subtracting 1 from os.date(*t) table, but it doesn’t work if the current date is 01/xx/2019.

    I also tried:

    os.date(“%Y-%m-%d”,os.time()-24*60*60)

    but getting this error

    attempt to perform arithmetic on a userdata value

    Thank you.

    There’s an example of date math near the end of this thread. Should be easy to adapt to your needs.

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com

    Hi Jeff, It worked. Thank you. You are awesome

    pastOrFuture = string.lower(pastOrFuture)
    local currrentDate = os.date(‘*t’)
    local atime = os.ts.time({year=currrentDate.year,month=currrentDate.month,day=currrentDate.day,hour=0,min=0,sec=0}) — create epoch time
    local adayInSeconds = (60 * 60 * 24*NumberOfDays )
    local btime
    if(pastOrFuture ==’past’) then
    btime = atime – adayInSeconds
    else
    btime = atime + adayInSeconds
    end
    local adate = os.ts.date(‘%d%m%Y’,btime)
    end

    Thank you

You must be logged in to reply to this topic.