5

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)
Neuron
  • 5,141
  • 5
  • 38
  • 59
Vijay
  • 71
  • 1
  • 2
  • 11

3 Answers3

5

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))
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
1

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.
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • first: `os.time` depends on your operating system and might not give you a value in seconds. Second: `3600 * 24 * 2` is not guaranteed to be exactly two days. There might be issues with winter savings time and certain leap years which change the length of a day every so slightly – Neuron Nov 28 '18 at 11:28
0

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
Vijay
  • 420
  • 6
  • 18