Kotlin

kotlinx-datetime — Multiplatform Dates

admin by @admin ADMIN
1h ago
Jun 1, 2026
Public
0 0 up · 0 down Sign in to vote
`kotlinx-datetime` is the multiplatform date/time library — works on JVM, JS, Native. `Instant` (UTC moment), `LocalDate`, `TimeZone` are the building blocks.
Kotlin
Raw
// build.gradle.kts:
//   implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.5.+")

import kotlinx.datetime.*

fun main() {
    // Now — as UTC instant
    val now: Instant = Clock.System.now()
    println(now)                                          // 2025-03-12T19:25:00Z

    // Local date in a specific timezone
    val tz = TimeZone.of("America/Chicago")
    val localNow: LocalDateTime = now.toLocalDateTime(tz)
    println(localNow)                                     // 2025-03-12T14:25:00.123

    // Date-only
    val today: LocalDate = localNow.date
    println(today.year)                                   // 2025
    println(today.dayOfWeek)                              // WEDNESDAY

    // Add / subtract — DateTimePeriod for calendar units
    val nextWeek = today.plus(DatePeriod(days = 7))
    val twoMonthsAgo = today.minus(2, DateTimeUnit.MONTH)
    println("$nextWeek, $twoMonthsAgo")

    // Duration between two Instants
    val later = now.plus(1, DateTimeUnit.HOUR, tz)
    val elapsed = later - now                              // kotlin.time.Duration
    println(elapsed)                                       // 1h
}
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.