Formatting Dates
Build readable date strings.
Formatting Dates is a free Lua Academy lesson on CoddyKit — lesson 2 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.
Shaping the Output
You already know os.date can format time. This lesson focuses on the format string itself: the small % codes that decide exactly how a date appears.
Mastering them lets you produce dates in any style, from 2024-03-09 to Saturday, March 9.
print(os.date("%Y-%m-%d"))Year, Month, Day
The core date codes are %Y for the 4-digit year, %m for the 2-digit month (01-12), and %d for the 2-digit day (01-31).
Anything that is not a code, like the dashes here, is printed exactly as written.
print(os.date("%Y/%m/%d"))
print(os.date("%d-%m-%Y"))Hours, Minutes, Seconds
For the clock, use %H for the hour in 24-hour form (00-23), %M for minutes, and %S for seconds.
Each is two digits, padded with a leading zero when needed, so times always line up neatly.
print(os.date("%H:%M:%S"))12-Hour Clock
Prefer AM/PM style? Use %I for the hour in 12-hour form (01-12) and %p for AM or PM.
Mixing them gives a friendly time like 03:45 PM that many people find easier to read.
print(os.date("%I:%M %p"))Names of Days
Lua can spell out weekdays. %A gives the full name like Monday, while %a gives the short form like Mon.
These names follow your system's locale, so they appear in the language your machine is set to.
print(os.date("%A"))
print(os.date("%a"))Names of Months
Months work the same way. %B gives the full month name like March, and %b gives the short form like Mar.
Combine names with numbers to build a headline-style date string.
print(os.date("%B %d, %Y"))A Friendly Full Date
By chaining codes and plain text, you can write almost any sentence. Here weekday, month, day, and year combine into a readable line.
Notice the comma and spaces are just ordinary characters between the codes.
print(os.date("%A, %B %d, %Y"))Formatting a Stored Time
Formatting is not limited to now. Pass a saved time number as the second argument and you can format any moment in the same style.
This is how logs show when each past event happened.
local t = os.time({year=2022, month=12, day=25})
print(os.date("%A, %B %d, %Y", t))Day of Year and Week
A few extra codes are useful for counting. %j gives the day of the year (001-366), and %w gives the weekday number where Sunday is 0.
These help with progress bars and scheduling logic.
local t = os.time({year=2024, month=2, day=1})
print("Day of year:", os.date("%j", t))
print("Weekday num:", os.date("%w", t))Escaping Percent
What if you want a literal percent sign in your output? Write %% and Lua prints a single %.
This avoids confusion when a percentage symbol sits next to a format code.
print(os.date("Loaded 100%% on %Y-%m-%d"))Building Filenames
A common trick is to put a timestamp into a filename. Codes like %Y%m%d_%H%M%S produce a sortable, space-free string.
Files named this way naturally sort in chronological order in any folder.
local name = os.date("backup_%Y%m%d.txt")
print(name)Quick Check
Pick the right code for the job.
Recap
Format strings combine % codes with plain text. Key codes: %Y %m %d for the date, %H %M %S for the time, and %A %B for names.
Use %I %p for 12-hour style, %% for a literal percent, and pass a time number to format past moments. With these you can shape dates any way you like.
Frequently asked questions
Is the “Formatting Dates” lesson free?
Yes — the full text of “Formatting Dates” 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 “Formatting Dates”?
Build readable date strings. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Formatting Dates” 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