
Convenient extensions and utilities for dates, times and timestamps: now getters, converters, arithmetic, queries, rounding, parsing, formatting, duration calculations and start/end-of-day helpers.
Kotlin Multiplatform library extending kotlinx-datetime functionality. Provides convenient extensions and utilities for working with dates and times.
This package is published to Maven Central Repository: kotlinx-datetime-ext on Maven Central
Depend on the latest version by declaring this in libs.versions.toml:
[versions]
kotlinx-datetime-ext = "0.2.0"
[libraries]
kotlinx-datetime-ext = { group = "io.github.kalist28", name = "kotlinx-datetime-ext", version.ref = "kotlinx-datetime-ext" }Or directly in build.gradle.kts:
dependencies {
implementation("io.github.kalist28:kotlinx-datetime-ext:0.2.0")
}val now = instantNow() // Instant
val today = dateNow() // LocalDate
val time = timeNow() // LocalTime
val dateTime = dateTimeNow() // LocalDateTime
val utcDateTime = dateTimeNow(TimeZone.UTC) // LocalDateTime in UTC// LocalDateTime/LocalDate β timestamp
val timestamp = dateTime.timestamp // UTC timestamp in milliseconds
val timestampUtc = dateTime.timestampUtc // System timezone timestamp
// timestamp β LocalDateTime/LocalDate
val dateTime = 1672574400000L.toDateTime() // System timezone
val dateTimeUtc = 1672574400000L.toLocalDateTime() // System timezone
val date = 1672574400000L.toLocalDate() // System timezone
val dateTime2 = 1672574400000L.asDateTime // System timezone (property)// Adding and subtracting Duration
val future = dateTime + 5.days
val past = dateTime - 3.hours
val futureDate = date + 2.days
val pastTime = time - 1.hours// Time checks
val isPast = dateTime.isPast // true if in the past
val isPastDate = date.isPast // true if date is in the past
// Day of week checks
val isHoliday = date.isHoliday // true if Saturday or Sunday
val isWeekday = date.isWeekday // true if weekday
// Year checks
val isLeap = date.isLeapYear // true if leap year
val isLeapYear = 2020.isLeap() // true if leap year
// Day counts
val daysInMonth = date.daysInMonth // Number of days in month
val daysInYear = date.daysInYear // Number of days in year (365 or 366)// Start and end of day
val start = date.atStartOfDay() // LocalDateTime(2023, 12, 17, 0, 0)
val end = date.atEndOfDay() // LocalDateTime(2023, 12, 17, 23, 59, 59, 999999999)
// Duration calculation
val duration = first durationUntil second // Duration between two LocalDateTime
val daysDuration = date1 durationUntil date2 // Duration between two LocalDate// Conversion to minutes/seconds
val minutes = time.minutes // Minutes since midnight
val seconds = time.seconds // Seconds since midnight
val timeFromMinutes = 150.asLocalTime // LocalTime from minutes
// Arithmetic
val diff = time1 minus time2 // Difference between times
val sum = time1 plus time2 // Sum of times (wraps around 24 hours)
// Rounding and formatting
val rounded = time.roundToHour // Round to nearest hour
val formatted = time.formatToClock() // Format to "HH:mm"
val parsed = "09:05".clockToLocalTime() // Parse from "HH:mm" or "HH:mm:ss" string| Platform | Status |
|---|---|
| Android | β |
| iOS | β |
| JVM | β |
| macOS | β |
| Linux | β |
| Windows | β |
| JavaScript | β |
| Wasm | β |
Kotlin Multiplatform library extending kotlinx-datetime functionality. Provides convenient extensions and utilities for working with dates and times.
This package is published to Maven Central Repository: kotlinx-datetime-ext on Maven Central
Depend on the latest version by declaring this in libs.versions.toml:
[versions]
kotlinx-datetime-ext = "0.2.0"
[libraries]
kotlinx-datetime-ext = { group = "io.github.kalist28", name = "kotlinx-datetime-ext", version.ref = "kotlinx-datetime-ext" }Or directly in build.gradle.kts:
dependencies {
implementation("io.github.kalist28:kotlinx-datetime-ext:0.2.0")
}val now = instantNow() // Instant
val today = dateNow() // LocalDate
val time = timeNow() // LocalTime
val dateTime = dateTimeNow() // LocalDateTime
val utcDateTime = dateTimeNow(TimeZone.UTC) // LocalDateTime in UTC// LocalDateTime/LocalDate β timestamp
val timestamp = dateTime.timestamp // UTC timestamp in milliseconds
val timestampUtc = dateTime.timestampUtc // System timezone timestamp
// timestamp β LocalDateTime/LocalDate
val dateTime = 1672574400000L.toDateTime() // System timezone
val dateTimeUtc = 1672574400000L.toLocalDateTime() // System timezone
val date = 1672574400000L.toLocalDate() // System timezone
val dateTime2 = 1672574400000L.asDateTime // System timezone (property)// Adding and subtracting Duration
val future = dateTime + 5.days
val past = dateTime - 3.hours
val futureDate = date + 2.days
val pastTime = time - 1.hours// Time checks
val isPast = dateTime.isPast // true if in the past
val isPastDate = date.isPast // true if date is in the past
// Day of week checks
val isHoliday = date.isHoliday // true if Saturday or Sunday
val isWeekday = date.isWeekday // true if weekday
// Year checks
val isLeap = date.isLeapYear // true if leap year
val isLeapYear = 2020.isLeap() // true if leap year
// Day counts
val daysInMonth = date.daysInMonth // Number of days in month
val daysInYear = date.daysInYear // Number of days in year (365 or 366)// Start and end of day
val start = date.atStartOfDay() // LocalDateTime(2023, 12, 17, 0, 0)
val end = date.atEndOfDay() // LocalDateTime(2023, 12, 17, 23, 59, 59, 999999999)
// Duration calculation
val duration = first durationUntil second // Duration between two LocalDateTime
val daysDuration = date1 durationUntil date2 // Duration between two LocalDate// Conversion to minutes/seconds
val minutes = time.minutes // Minutes since midnight
val seconds = time.seconds // Seconds since midnight
val timeFromMinutes = 150.asLocalTime // LocalTime from minutes
// Arithmetic
val diff = time1 minus time2 // Difference between times
val sum = time1 plus time2 // Sum of times (wraps around 24 hours)
// Rounding and formatting
val rounded = time.roundToHour // Round to nearest hour
val formatted = time.formatToClock() // Format to "HH:mm"
val parsed = "09:05".clockToLocalTime() // Parse from "HH:mm" or "HH:mm:ss" string| Platform | Status |
|---|---|
| Android | β |
| iOS | β |
| JVM | β |
| macOS | β |
| Linux | β |
| Windows | β |
| JavaScript | β |
| Wasm | β |