Time Arithmetic
Add and compare times.
Time Arithmetic is a free Lua Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Time Is Just Numbers
Because os.time returns plain seconds, doing date math is mostly ordinary arithmetic. Add seconds to move forward, subtract to look back.
This lesson shows how to shift times, compare them, and find the gap between two moments.
Seconds in a Minute, Hour, Day
To work in larger units, remember the basic counts: 60 seconds in a minute, 3600 in an hour, and 86400 in a day.
Multiplying by these constants lets you express any duration in seconds.
print("Minute:", 60)
print("Hour:", 60 * 60)
print("Day:", 24 * 60 * 60)One Hour Later
To get the time one hour from now, take os.time() and add 3600 seconds.
Feed the new number to os.date to see the result as readable text.
local now = os.time()
local later = now + 3600
print(os.date("%H:%M", now))
print(os.date("%H:%M", later))Yesterday and Tomorrow
Add 86400 seconds to jump a day ahead, or subtract it to step a day back.
This is a quick way to compute tomorrow's or yesterday's date without worrying about month boundaries.
local now = os.time()
print("Yesterday:", os.date("%Y-%m-%d", now - 86400))
print("Tomorrow: ", os.date("%Y-%m-%d", now + 86400))Building a Specific Date
Start arithmetic from a fixed date by building it with a table. Here we create New Year's Day 2025 and then add a week.
The added time correctly rolls into January 8.
local start = os.time({year=2025, month=1, day=1})
local week = start + 7 * 86400
print(os.date("%Y-%m-%d", week))Comparing Two Times
Since times are numbers, the normal comparison operators work. A larger number means a later moment.
This makes it easy to check whether a deadline has passed.
local deadline = os.time({year=2020, month=1, day=1})
local now = os.time()
if now > deadline then
print("Deadline has passed")
endDifference in Seconds
Subtracting one time from another gives the gap in seconds. Lua also offers os.difftime(a, b) which returns a - b as a number.
Using os.difftime is the portable, recommended way to find a span.
local a = os.time({year=2025, month=1, day=2})
local b = os.time({year=2025, month=1, day=1})
print(os.difftime(a, b))Turning Seconds Into Days
A raw second gap is large, so divide to reach friendlier units. Divide by 86400 for days, or by 3600 for hours.
Use math.floor to drop the fractional part when you only want whole units.
local diff = os.difftime(
os.time({year=2025, month=1, day=11}),
os.time({year=2025, month=1, day=1}))
print(diff / 86400, "days")Days Until an Event
Combine the ideas to count down to a future date. Subtract now from the target, then convert seconds to whole days.
This pattern powers countdown timers and reminder apps.
local target = os.time({year=2030, month=1, day=1})
local secs = os.difftime(target, os.time())
local days = math.floor(secs / 86400)
print("Days left:", days)Normalizing With os.time
A neat trick: give os.time a table with an out-of-range field and it fixes it. Day 32 of January becomes February 1.
This lets you add days by simply increasing the day field, and Lua sorts out the calendar.
local t = os.time({year=2025, month=1, day=32})
print(os.date("%Y-%m-%d", t))Add a Month Safely
Because of normalization, you can bump the month field past 12 and Lua rolls the year forward.
Month 13 of 2025 becomes January 2026 automatically.
local t = os.time({year=2025, month=13, day=1})
print(os.date("%Y-%m-%d", t))Quick Check
Apply your time-math knowledge.
Recap
Time values are numbers, so add or subtract seconds to shift them: 3600 per hour, 86400 per day. Compare times directly with > and <.
Use os.difftime for the gap between two times, then divide to get hours or days. And remember os.time normalizes out-of-range table fields, making calendar math simple.
Frequently asked questions
Is the “Time Arithmetic” lesson free?
Yes — the full text of “Time Arithmetic” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “Time Arithmetic”?
Add and compare times. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Time Arithmetic” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- os.time and os.date
- Formatting Dates
- Time Arithmetic
- Measuring Durations