Dates with kotlinx-datetime
A multiplatform-safe way to handle time and dates.
Dates with kotlinx-datetime is a free Kotlin Multiplatform 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 Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Dates Need a Library
The core stdlib has no shared date type, so KMP uses kotlinx-datetime, an official multiplatform library for time.
Add the Dependency
You pull kotlinx-datetime into commonMain in your build file, and then every target can work with dates the same way.
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.0")Instant Is a Moment
An Instant is a single point on the timeline, the same exact moment no matter which device reads it.
val now: Instant = Clock.System.now()Time Zones Matter
The same instant looks different per TimeZone. The library lets you convert moments into local wall-clock time safely.
val zone = TimeZone.currentSystemDefault()LocalDateTime for People
A LocalDateTime is the human-readable date and time, like a calendar entry, without any time-zone baggage attached.
val dt = now.toLocalDateTime(zone)Read the Parts
From a LocalDateTime you can read fields like year, month, day and hour, all in plain common Kotlin.
val y = dt.year
val h = dt.hourLocalDate for Calendars
When you only care about the day, use LocalDate. It models a date with no time component at all.
val today: LocalDate = dt.dateDo Math With Durations
You can add or subtract a Duration from an Instant to shift moments forward or backward in time.
val later = now.plus(1, DateTimeUnit.HOUR, zone)Parse and Format Text
kotlinx-datetime can parse ISO strings into instants and turn them back into text, ideal for API payloads.
val parsed = Instant.parse("2024-01-01T00:00:00Z")Identical On Every Target
Because the library is multiplatform, your date logic produces the same result on Android, iOS and desktop. 🙂
Keep Dates in Shared Code
Put all your time handling in commonMain with kotlinx-datetime, so the two apps never disagree about dates.
Quick Check
Which type marks one exact moment in time?
Recap
Use kotlinx-datetime for dates: Instant for moments, LocalDateTime for people, and shared code that agrees across platforms.
Frequently asked questions
Is the “Dates with kotlinx-datetime” lesson free?
Yes — the full text of “Dates with kotlinx-datetime” is free to read here on the web, and the Kotlin Multiplatform 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 Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “Dates with kotlinx-datetime”?
A multiplatform-safe way to handle time and dates. You practise Kotlin Multiplatform 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 Kotlin Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform 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 “Dates with kotlinx-datetime” 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 Kotlin Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform 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
- Strings, Numbers & Collections Everywhere
- map, filter & fold in Shared Code
- Dates with kotlinx-datetime
- What Is NOT in Common stdlib