Your specification is not completely clear. I guess the following script may be helpful.
The trick is to convert your dates to time values, as returned by os.time
, which can be compared as simple numbers.
To do this you first have to parse the string form of the dates and then convert them in a table form suitable to be fed to os.time
. Note that incrementing the fields of those tables (which represent the dates with their components split) already handles the time arithmetic, i.e. specifying a day
field with a value of, for example, 32
will wrap correctly to the next month when converted to time values.
When you reach the target date you convert the time value back to a date value in the format you need using os.date
.
local TARGET_DATE = "03/05/2012"
-- get the two dates from the command line of this lua script
local date1 = arg[1]
local date2 = arg[2]
date1 = "01/01/2012" -- test data - remove in actual script
date2 = "02/01/2012" -- test data - remove in actual script
-- parse the dates (assuming the format day/month/year)
local d1, m1, y1 = string.match( date1, '^(%d%d)/(%d%d)/(%d%d%d%d)$' )
local d2, m2, y2 = string.match( date2, '^(%d%d)/(%d%d)/(%d%d%d%d)$' )
local td, tm, ty = string.match( TARGET_DATE, '^(%d%d)/(%d%d)/(%d%d%d%d)$' )
print( d1, m1, y1 ) -- debug
print( d2, m2, y2 ) -- debug
print( td, tm, ty ) -- debug
date1_tbl = { day = d1, month = m1, year = y1 }
date2_tbl = { day = d2, month = m2, year = y2 }
local time1 = os.time( date1_tbl )
local time2 = os.time( date2_tbl )
local target_time = os.time { day = td, month = tm, year = ty }
-- loop until both dates exceed target date
while time1 < target_time or time2 < target_time do
date1_tbl.day = date1_tbl.day + 14
date2_tbl.day = date2_tbl.day + 14
time1 = os.time( date1_tbl )
time2 = os.time( date2_tbl )
end
-- rebuild new dates
local newdate1 = os.date( "%d/%m/%Y", time1 )
local newdate2 = os.date( "%d/%m/%Y", time2 )
print( newdate1 ) -- debug
print( newdate2 ) -- debug