Hi All, just joined the channel. Wanted to know ho...
# kotlinx-datetime
n
Hi All, just joined the channel. Wanted to know how to get the days difference between two dates in Kotlin.LocalDate. for example :
Copy code
val dt1= LocalDate(2024,5,15)
val dt2 = LocalDate(2024,2,23)
How to find the difference in number of days?
n
Thanks for your response @Stylianos Gakis When I tried this code below:
Copy code
val dt1= LocalDate(2024,5,15)
val dt2 = LocalDate(2024,2,23)

val rdf = dt1.minus(dt2)

println(rdf)
What I get in the console is: P2M22D , which I think must be referring to 2 months, 22 days. However, that is not an absolute number, since two months could mean different for different months. How do I get it in absolute days?
s
Those functions seem to respond with a
DatePeriod
object right? https://github.com/Kotlin/kotlinx-datetime/blob/bc8adee2b9e3659e8e1c38fc09f3a4a1bdf85276/core/common/src/DateTimePeriod.kt#L430 This seems to show that it has a
val days: Int
value in there you can grab.
n
well, the property 'days' don't give that. That only provides the days as in remainder days after month. In this case, 22 days after 2 months. However, I saw couple of functions there i.e., daysUntil/until which provided the right answer 82 days.
Copy code
val dt1= LocalDate(2024,5,15)
val dt2 = LocalDate(2024,2,23)

val rdf = dt2.daysUntil(dt1)

val rdf2 = dt2.daysUntil(dt1)

println(rdf)
Now it prints 82. Thank you @Stylianos Gakis so much for guiding & pointing to the right source. This helped me a lot!
🌟 1
s
That only provides the days as in remainder days after month
Ah I see, it was a guess after all 😅 Awesome to see it work!
👍 1
m