yearsUntil

actual fun LocalDate.yearsUntil(other: LocalDate): Int(source)
fun Instant.yearsUntil(other: Instant, timeZone: TimeZone): Int(source)

Returns the number of whole years between two instants in the specified timeZone.

If the result does not fit in Int, returns Int.MAX_VALUE for a positive result or Int.MIN_VALUE for a negative result.

See also

Throws

if this or other instant is too large to fit in LocalDateTime.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.random.*
import kotlin.test.*
import kotlin.time.Duration.Companion.hours
fun main() { 
   //sampleStart 
   // Finding the number of full years between two instants in the given time zone
val startInstant = Instant.parse("2024-03-01T02:01:00Z")
val endInstant = Instant.parse("2025-03-01T02:01:00Z")
// In New York, we find the difference between 2024-02-29 and 2025-02-28, which is just short of a year
val yearsBetweenInNewYork = startInstant.yearsUntil(endInstant, TimeZone.of("America/New_York"))
check(yearsBetweenInNewYork == 0)
// In Berlin, we find the difference between 2024-03-01 and 2025-03-01, which is exactly a year
val yearsBetweenInBerlin = startInstant.yearsUntil(endInstant, TimeZone.of("Europe/Berlin"))
check(yearsBetweenInBerlin == 1) 
   //sampleEnd
}

expect fun LocalDate.yearsUntil(other: LocalDate): Int(source)

Returns the number of whole years between two dates.

The value is rounded toward zero.

If the result does not fit in Int, returns Int.MAX_VALUE for a positive result or Int.MIN_VALUE for a negative result.

See also

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.random.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   // Finding how many years have passed between two dates
val dateOfBirth = LocalDate(2016, Month.JANUARY, 14)
val today = LocalDate(2024, Month.APRIL, 16)
val age = dateOfBirth.yearsUntil(today)
check(age == 8) 
   //sampleEnd
}
actual fun LocalDate.yearsUntil(other: LocalDate): Int(source)
actual fun LocalDate.yearsUntil(other: LocalDate): Int(source)