Question

I am new to lua scripting. I have a startDate ("03-05-2014" as "dd-mm-yyyy") and a span in days (2) Can anyone help me how to get the endDate based on the startDate and span?.

Example startDate     span            endDate 
        ---------     ----            -------
        03-05-2014     2             05-05-2014
       (dd-mm-yyyy)                 (dd-mm-2014)
Was it helpful?

Solution 3

This could help you

local dayValue, monthValue, yearValue = string.match('31-05-2014', '(%d%d)-(%d%d)-(%d%d%d%d)')
dayValue, monthValue, yearValue = tonumber(dayValue), tonumber(monthValue), tonumber(yearValue)
now = os.time{year = yearValue, month = monthValue, day = dayValue}
numberOfDays = now + 2 * 24 * 3600
print(os.date("%c",numberOfDays))
dateAfterNumberOfDays = os.date("%a %d %B %Y, %H%p%M",numberOfDays)
print ("\nafter number of days "..dateAfterNumberOfDays) -- give you date after number of days

OTHER TIPS

You don't need to do any math here. os.time and os.date will do it for you.

local day, month, year = ("03-05-2014"):match("(%d%d)-(%d%d)-(%d%d%d%d)")
local span = 64
local endtime = os.time({day = day + span, month = month, year = year})
print(os.date("%c", endtime))

I'm not going to write the whole program for you, but here's something you can start with:

  1. Get the day, month and year from the string:

    local day, month, year = string.match('03-05-2014', '(%d%d)-(%d%d)-(%d%d%d%d)')
    day, month, year = tonumber(day), tonumber(month), tonumber(year)
    
  2. Use os.time to get the timestamp of a start time. You can then add 3600 * 24 * 2 seconds (2 days) to get the timestamp of the end time.
  3. Use os.date to formats the string from a timestamp.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top