Kotlin .daysUntil()
Published Nov 13, 2023Updated Nov 17, 2023
The .daysUntil() method calculates the number of whole days between two instants and returns the result as an Int. If the number of days is too large to fit within the bounds of the Int datatype, the method will return Int.MAX_VALUE if the result is positive or Int.MIN_VALUE if the result is negative.
Syntax
firstInstant.daysUntil(secondInstant, TimeZone)
firstInstant: The first reference time.secondInstant: The second time.TimeZone: The timezone that the calculation should consider zone when determining the number of days between the twoInstantobjects.
Example
The example demonstrates the use of .daysUntil() to calculate the number of days between two Instants.
import kotlinx.datetime.*fun main() {val start = Instant.parse("2024-01-01T12:00:00Z")val end = Instant.parse("2023-11-01T12:00:00Z")val defaultTZ = TimeZone.currentSystemDefault()val timeDelta = end.daysUntil(start, defaultTZ)println(timeDelta)}
The output of this code will be:
60
Learn Kotlin on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
- Beginner Friendly.9 hours