Reputation: 71
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)
Upvotes: 5
Views: 6099
Reputation: 80921
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))
Upvotes: 5
Reputation: 122373
I'm not going to write the whole program for you, but here's something you can start with:
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)
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.os.date
to formats the string from a timestamp.Upvotes: 1
Reputation: 420
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
Upvotes: 0