A multiplatform Kotlin library for working with date and time, specifically the iOS x64 target variant
—
Civil date and time types for representing calendar dates and clock times without timezone reference. These types represent "civil time" - the time you would see on a clock or calendar, independent of time zone.
Represents a date without time-of-day or time zone reference.
/**
* The date part of LocalDateTime without time zone reference
* Represents a date on the calendar (year, month, day)
*/
expect class LocalDate : Comparable<LocalDate> {
/** Create a date from year, month (1-12), and day */
constructor(year: Int, month: Int, day: Int)
/** Create a date from year, month enum, and day */
constructor(year: Int, month: Month, day: Int)
/** Year component (can be negative for BCE dates) */
val year: Int
/** Month as enum value */
val month: Month
/** Day of month (1-31) */
val day: Int
/** Day of week as enum value */
val dayOfWeek: DayOfWeek
/** Day of year (1-366) */
val dayOfYear: Int
/** Month as numeric value (1-12) */
val monthNumber: Int
/** Day of month (1-31) - alias for day */
val dayOfMonth: Int
}Factory Functions:
/**
* Create LocalDate from epoch days (days since 1970-01-01)
* @param epochDays Number of days since epoch
* @returns LocalDate representing the date
*/
fun LocalDate.Companion.fromEpochDays(epochDays: Long): LocalDate
fun LocalDate.Companion.fromEpochDays(epochDays: Int): LocalDate
/**
* Parse LocalDate from string using specified format
* @param input String to parse
* @param format Format to use for parsing
* @returns Parsed LocalDate
* @throws IllegalArgumentException if parsing fails
*/
fun LocalDate.Companion.parse(input: CharSequence, format: DateTimeFormat<LocalDate>): LocalDate
/**
* Convenience constructors for LocalDate
*/
fun LocalDate(year: Int, monthNumber: Int, dayOfMonth: Int): LocalDate
fun LocalDate(year: Int, month: Month, dayOfMonth: Int): LocalDate
/**
* Parse LocalDate from string using ISO format
* @param input String in ISO format (YYYY-MM-DD)
* @returns Parsed LocalDate
*/
fun String.toLocalDate(): LocalDateDate Arithmetic:
/**
* Add a date period to this date
* @param period Period containing years, months, and/or days
* @returns New LocalDate after adding the period
*/
fun LocalDate.plus(period: DatePeriod): LocalDate
/**
* Subtract a date period from this date
* @param period Period to subtract
* @returns New LocalDate after subtracting the period
*/
fun LocalDate.minus(period: DatePeriod): LocalDate
/**
* Add a value of the specified date-based unit
* @param value Amount to add
* @param unit Date-based unit (DAY, WEEK, MONTH, YEAR, etc.)
* @returns New LocalDate after adding the value
*/
fun LocalDate.plus(value: Int, unit: DateTimeUnit.DateBased): LocalDate
fun LocalDate.plus(value: Long, unit: DateTimeUnit.DateBased): LocalDate
/**
* Subtract a value of the specified date-based unit
* @param value Amount to subtract
* @param unit Date-based unit
* @returns New LocalDate after subtracting the value
*/
fun LocalDate.minus(value: Int, unit: DateTimeUnit.DateBased): LocalDate
fun LocalDate.minus(value: Long, unit: DateTimeUnit.DateBased): LocalDate
/**
* Add one unit of the specified date-based unit
* @param unit Date-based unit to add
* @returns New LocalDate after adding one unit
*/
fun LocalDate.plus(unit: DateTimeUnit.DateBased): LocalDate
/**
* Subtract one unit of the specified date-based unit
* @param unit Date-based unit to subtract
* @returns New LocalDate after subtracting one unit
*/
fun LocalDate.minus(unit: DateTimeUnit.DateBased): LocalDatePeriod Calculations:
/**
* Calculate the number of whole units between this date and another
* @param other Target date
* @param unit Unit to measure in
* @returns Number of whole units between the dates
*/
fun LocalDate.until(other: LocalDate, unit: DateTimeUnit.DateBased): Long
/**
* Calculate number of days between this date and another
* @param other Target date
* @returns Number of days (can be negative if other is earlier)
*/
fun LocalDate.daysUntil(other: LocalDate): Int
/**
* Calculate number of months between this date and another
* @param other Target date
* @returns Number of months
*/
fun LocalDate.monthsUntil(other: LocalDate): Int
/**
* Calculate number of years between this date and another
* @param other Target date
* @returns Number of years
*/
fun LocalDate.yearsUntil(other: LocalDate): Int
/**
* Calculate the date period between this date and another
* @param other Target date
* @returns DatePeriod representing the difference
*/
fun LocalDate.periodUntil(other: LocalDate): DatePeriodTime Combination:
/**
* Combine this date with time components to create LocalDateTime
* @param hour Hour (0-23)
* @param minute Minute (0-59)
* @param second Second (0-59), defaults to 0
* @param nanosecond Nanosecond (0-999,999,999), defaults to 0
* @returns LocalDateTime combining this date with the time
*/
fun LocalDate.atTime(hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0): LocalDateTime
/**
* Combine this date with a LocalTime to create LocalDateTime
* @param time The time component
* @returns LocalDateTime combining this date with the time
*/
fun LocalDate.atTime(time: LocalTime): LocalDateTime
/**
* Get the instant representing the start of this date in the given time zone
* @param timeZone Time zone to convert in
* @returns Instant representing midnight of this date in the time zone
*/
fun LocalDate.atStartOfDayIn(timeZone: TimeZone): InstantConversions:
/**
* Convert this date to epoch days (days since 1970-01-01)
* @returns Number of days since epoch
*/
fun LocalDate.toEpochDays(): LongRange Operations:
/**
* Create an inclusive range from this date to another
* @param that End date (inclusive)
* @returns LocalDateRange from this date to that date
*/
fun LocalDate.rangeTo(that: LocalDate): LocalDateRange
/**
* Create a range from this date up to (but not including) another
* @param that End date (exclusive)
* @returns LocalDateRange from this date until that date
*/
fun LocalDate.rangeUntil(that: LocalDate): LocalDateRangeFormatting:
/**
* Format this date using the specified format
* @param format Format to use
* @returns Formatted string representation
*/
fun LocalDate.format(format: DateTimeFormat<LocalDate>): String
/**
* Convert to ISO 8601 string representation (YYYY-MM-DD)
* @returns ISO format string
*/
override fun LocalDate.toString(): StringUsage Examples:
import kotlinx.datetime.*
// Create dates
val date1 = LocalDate(2023, 12, 25)
val date2 = LocalDate(2023, Month.DECEMBER, 25)
val fromEpoch = LocalDate.fromEpochDays(19723) // 2024-01-01
// Date arithmetic
val tomorrow = date1.plus(DatePeriod(days = 1))
val nextMonth = date1.plus(1, DateTimeUnit.MONTH)
val nextYear = date1.plus(1, DateTimeUnit.YEAR)
// Calculate differences
val daysBetween = date1.daysUntil(tomorrow) // 1
val period = date1.periodUntil(nextYear) // DatePeriod(years=1)
// Combine with time
val dateTime = date1.atTime(14, 30, 15)
val withTime = date1.atTime(LocalTime(9, 0))
// Time zone conversion
val timeZone = TimeZone.of("America/New_York")
val instant = date1.atStartOfDayIn(timeZone)
// Ranges
val dateRange = date1..date1.plus(DatePeriod(days = 7))
for (day in dateRange) {
println(day)
}Represents time-of-day without date or time zone reference.
/**
* Time-of-day without date or time zone reference
* Represents clock time (hour, minute, second, nanosecond)
*/
expect class LocalTime : Comparable<LocalTime> {
/** Create time from components */
constructor(hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0)
/** Hour of day (0-23) */
val hour: Int
/** Minute of hour (0-59) */
val minute: Int
/** Second of minute (0-59) */
val second: Int
/** Nanosecond of second (0-999,999,999) */
val nanosecond: Int
}Factory Functions:
/**
* Create LocalTime from second of day
* @param secondOfDay Seconds since midnight (0-86399)
* @returns LocalTime representing the time
*/
fun LocalTime.Companion.fromSecondOfDay(secondOfDay: Int): LocalTime
/**
* Create LocalTime from millisecond of day
* @param millisecondOfDay Milliseconds since midnight
* @returns LocalTime representing the time
*/
fun LocalTime.Companion.fromMillisecondOfDay(millisecondOfDay: Int): LocalTime
/**
* Create LocalTime from nanosecond of day
* @param nanosecondOfDay Nanoseconds since midnight
* @returns LocalTime representing the time
*/
fun LocalTime.Companion.fromNanosecondOfDay(nanosecondOfDay: Long): LocalTime
/**
* Parse LocalTime from string using specified format
* @param input String to parse
* @param format Format to use for parsing
* @returns Parsed LocalTime
*/
fun LocalTime.Companion.parse(input: CharSequence, format: DateTimeFormat<LocalTime>): LocalTime
/**
* Parse LocalTime from string using ISO format
* @param input String in ISO format (HH:MM:SS)
* @returns Parsed LocalTime
*/
fun String.toLocalTime(): LocalTimeConversions:
/**
* Convert to seconds since midnight
* @returns Seconds since midnight (0-86399)
*/
fun LocalTime.toSecondOfDay(): Int
/**
* Convert to milliseconds since midnight
* @returns Milliseconds since midnight
*/
fun LocalTime.toMillisecondOfDay(): Int
/**
* Convert to nanoseconds since midnight
* @returns Nanoseconds since midnight
*/
fun LocalTime.toNanosecondOfDay(): LongDate Combination:
/**
* Combine this time with date components to create LocalDateTime
* @param year Year
* @param month Month (1-12)
* @param day Day of month
* @returns LocalDateTime combining the date with this time
*/
fun LocalTime.atDate(year: Int, month: Int, day: Int): LocalDateTime
fun LocalTime.atDate(year: Int, month: Month, day: Int): LocalDateTime
/**
* Alternative overloads with explicit parameter names for disambiguation
*/
fun LocalTime.atDate(year: Int, monthNumber: Int, dayOfMonth: Int): LocalDateTime
fun LocalTime.atDate(year: Int, month: Month, dayOfMonth: Int): LocalDateTime
/**
* Combine this time with a LocalDate to create LocalDateTime
* @param date The date component
* @returns LocalDateTime combining the date with this time
*/
fun LocalTime.atDate(date: LocalDate): LocalDateTimeFormatting:
/**
* Format this time using the specified format
* @param format Format to use
* @returns Formatted string representation
*/
fun LocalTime.format(format: DateTimeFormat<LocalTime>): String
/**
* Convert to ISO 8601 string representation (HH:MM:SS or HH:MM:SS.sss)
* @returns ISO format string
*/
override fun LocalTime.toString(): StringUsage Examples:
import kotlinx.datetime.*
// Create times
val time1 = LocalTime(14, 30, 15)
val time2 = LocalTime(9, 0) // seconds and nanoseconds default to 0
val fromSeconds = LocalTime.fromSecondOfDay(52215) // 14:30:15
// Conversions
val secondsOfDay = time1.toSecondOfDay() // 52215
val millisOfDay = time1.toMillisecondOfDay()
val nanosOfDay = time1.toNanosecondOfDay()
// Combine with date
val date = LocalDate(2023, 12, 25)
val dateTime1 = time1.atDate(date)
val dateTime2 = time1.atDate(2023, 12, 25)Represents civil date and time without time zone reference.
/**
* Civil date and time without time zone reference
* Combines LocalDate and LocalTime into a single type
*/
expect class LocalDateTime : Comparable<LocalDateTime> {
/** Create from individual components */
constructor(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0)
constructor(year: Int, month: Month, day: Int, hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0)
/** Create from date and time components */
constructor(date: LocalDate, time: LocalTime)
/** Year component */
val year: Int
/** Month as enum value */
val month: Month
/** Day of month (1-31) */
val day: Int
/** Day of week as enum value */
val dayOfWeek: DayOfWeek
/** Day of year (1-366) */
val dayOfYear: Int
/** Hour of day (0-23) */
val hour: Int
/** Minute of hour (0-59) */
val minute: Int
/** Second of minute (0-59) */
val second: Int
/** Nanosecond of second (0-999,999,999) */
val nanosecond: Int
/** Date part of this LocalDateTime */
val date: LocalDate
/** Time part of this LocalDateTime */
val time: LocalTime
}Factory Functions:
/**
* Parse LocalDateTime from string using specified format
* @param input String to parse
* @param format Format to use for parsing
* @returns Parsed LocalDateTime
*/
fun LocalDateTime.Companion.parse(input: CharSequence, format: DateTimeFormat<LocalDateTime>): LocalDateTimeTime Zone Conversions:
/**
* Convert this LocalDateTime to an Instant in the specified time zone
* @param timeZone Time zone for conversion
* @returns Instant representing this LocalDateTime in the time zone
*/
fun LocalDateTime.toInstant(timeZone: TimeZone): Instant
/**
* Convert this LocalDateTime to an Instant using the specified offset
* @param offset UTC offset for conversion
* @returns Instant representing this LocalDateTime with the offset
*/
fun LocalDateTime.toInstant(offset: UtcOffset): InstantFormatting:
/**
* Format this LocalDateTime using the specified format
* @param format Format to use
* @returns Formatted string representation
*/
fun LocalDateTime.format(format: DateTimeFormat<LocalDateTime>): String
/**
* Convert to ISO 8601 string representation (YYYY-MM-DDTHH:MM:SS)
* @returns ISO format string
*/
override fun LocalDateTime.toString(): StringUsage Examples:
import kotlinx.datetime.*
// Create LocalDateTime
val dateTime1 = LocalDateTime(2023, 12, 25, 14, 30, 15)
val dateTime2 = LocalDateTime(2023, Month.DECEMBER, 25, 14, 30, 15)
val date = LocalDate(2023, 12, 25)
val time = LocalTime(14, 30, 15)
val dateTime3 = LocalDateTime(date, time)
// Access components
println("Year: ${dateTime1.year}")
println("Month: ${dateTime1.month}")
println("Day: ${dateTime1.day}")
println("Hour: ${dateTime1.hour}")
println("Date part: ${dateTime1.date}")
println("Time part: ${dateTime1.time}")
// Convert to Instant
val timeZone = TimeZone.of("America/New_York")
val instant = dateTime1.toInstant(timeZone)
val offset = UtcOffset(hours = -5)
val instantWithOffset = dateTime1.toInstant(offset)Represents year-month part without day-of-month, useful for representing months or for month-based operations.
/**
* Year-month part without day-of-month
* Represents a specific month in a specific year
*/
expect class YearMonth : Comparable<YearMonth> {
/** Create from year and month number */
constructor(year: Int, month: Int)
/** Create from year and month enum */
constructor(year: Int, month: Month)
/** Year component */
val year: Int
/** Month as enum value */
val month: Month
/** First day of this month */
val firstDay: LocalDate
/** Last day of this month */
val lastDay: LocalDate
/** Number of days in this month */
val numberOfDays: Int
/** Range of all days in this month */
val days: LocalDateRange
}Factory Functions:
/**
* Parse YearMonth from string using specified format
* @param input String to parse
* @param format Format to use for parsing
* @returns Parsed YearMonth
*/
fun YearMonth.Companion.parse(input: CharSequence, format: DateTimeFormat<YearMonth>): YearMonthMonth Arithmetic:
/**
* Add a value of the specified month-based unit
* @param value Amount to add
* @param unit Month-based unit (MONTH, QUARTER, YEAR, etc.)
* @returns New YearMonth after adding the value
*/
fun YearMonth.plus(value: Int, unit: DateTimeUnit.MonthBased): YearMonth
fun YearMonth.plus(value: Long, unit: DateTimeUnit.MonthBased): YearMonth
/**
* Subtract a value of the specified month-based unit
* @param value Amount to subtract
* @param unit Month-based unit
* @returns New YearMonth after subtracting the value
*/
fun YearMonth.minus(value: Int, unit: DateTimeUnit.MonthBased): YearMonth
fun YearMonth.minus(value: Long, unit: DateTimeUnit.MonthBased): YearMonthPeriod Calculations:
/**
* Calculate the number of whole units between this YearMonth and another
* @param other Target YearMonth
* @param unit Unit to measure in
* @returns Number of whole units between the months
*/
fun YearMonth.until(other: YearMonth, unit: DateTimeUnit.MonthBased): Long
/**
* Calculate number of months between this YearMonth and another
* @param other Target YearMonth
* @returns Number of months
*/
fun YearMonth.monthsUntil(other: YearMonth): Int
/**
* Calculate number of years between this YearMonth and another
* @param other Target YearMonth
* @returns Number of years
*/
fun YearMonth.yearsUntil(other: YearMonth): IntDate Creation:
/**
* Create a LocalDate by specifying the day of this month
* @param day Day of month (1 to numberOfDays)
* @returns LocalDate for the specified day of this month
*/
fun YearMonth.onDay(day: Int): LocalDateConvenience Functions:
/**
* Add one year to this YearMonth
* @returns YearMonth representing next year, same month
*/
fun YearMonth.plusYear(): YearMonth
/**
* Subtract one year from this YearMonth
* @returns YearMonth representing previous year, same month
*/
fun YearMonth.minusYear(): YearMonth
/**
* Add one month to this YearMonth
* @returns YearMonth representing next month
*/
fun YearMonth.plusMonth(): YearMonth
/**
* Subtract one month from this YearMonth
* @returns YearMonth representing previous month
*/
fun YearMonth.minusMonth(): YearMonthRange Operations:
/**
* Create an inclusive range from this YearMonth to another
* @param that End YearMonth (inclusive)
* @returns YearMonthRange from this month to that month
*/
fun YearMonth.rangeTo(that: YearMonth): YearMonthRange
/**
* Create a range from this YearMonth up to (but not including) another
* @param that End YearMonth (exclusive)
* @returns YearMonthRange from this month until that month
*/
fun YearMonth.rangeUntil(that: YearMonth): YearMonthRangeExtension Properties:
/**
* Get the YearMonth for this LocalDate
*/
val LocalDate.yearMonth: YearMonthUsage Examples:
import kotlinx.datetime.*
// Create YearMonth
val ym1 = YearMonth(2023, 12)
val ym2 = YearMonth(2023, Month.DECEMBER)
// Access properties
println("Year: ${ym1.year}")
println("Month: ${ym1.month}")
println("First day: ${ym1.firstDay}") // 2023-12-01
println("Last day: ${ym1.lastDay}") // 2023-12-31
println("Days in month: ${ym1.numberOfDays}") // 31
// Create dates in this month
val christmas = ym1.onDay(25) // 2023-12-25
// Month arithmetic
val nextMonth = ym1.plusMonth() // 2024-01
val nextYear = ym1.plusYear() // 2024-12
val add6Months = ym1.plus(6, DateTimeUnit.MONTH) // 2024-06
// Iterate over days in month
for (day in ym1.days) {
println(day)
}
// Get YearMonth from LocalDate
val date = LocalDate(2023, 12, 25)
val yearMonth = date.yearMonth // 2023-12Each local type provides predefined formatting constants:
// LocalDate formats
object LocalDate.Formats {
val ISO: DateTimeFormat<LocalDate> // YYYY-MM-DD
val ISO_BASIC: DateTimeFormat<LocalDate> // YYYYMMDD
}
// LocalTime formats
object LocalTime.Formats {
val ISO: DateTimeFormat<LocalTime> // HH:MM:SS or HH:MM:SS.sss
}
// LocalDateTime formats
object LocalDateTime.Formats {
val ISO: DateTimeFormat<LocalDateTime> // YYYY-MM-DDTHH:MM:SS
}
// YearMonth formats
object YearMonth.Formats {
val ISO: DateTimeFormat<YearMonth> // YYYY-MM
}Install with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-kotlinx--kotlinx-datetime-iosx64