https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
r

Raed Ghazal

10/08/2023, 8:54 AM
Hi, idk if its allowed to share articles here, but I’ve written one about working with
kotlinx.datetime
might be helpful to others, specially if you’re moving from
java.time
package and struggling to find alternatives for its usages https://raed-o-ghazal.medium.com/kotlinx-localdatetime-manipulation-for-kmm-eacfede93aba
K 3
c

Christiano

11/24/2023, 9:24 AM
Nice article! What I do miss still is how formatting a Date can be done in the js side of things. I always get an error that the
js-joda/locale
is not added, but can't seem to find anything regarding that in conjunction with KMP 😅
r

Raed Ghazal

11/24/2023, 10:12 AM
Glad it helped, hmm not sure about JS, for what platform are you using KMP?
but I think it shouldn’t be complicated, try transforming the kotlin date object to a JS object then use native JS formatting
c

Christiano

11/24/2023, 10:36 AM
I am actually using a framework Kobweb to create a website with a DOM structure. With that last sentence I'm afraid you lost me. First real time that I'm touching KMP and feels like a whole different world than what I am used to (native Android) xD
😅 1
I think I'm already doing what you suggested. I have this snippet
Copy code
val formatter = DateTimeFormatter.ofPattern("MMMM yyyy")
val fromDate = LocalDateTime.parse(from, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"))
val toDate = LocalDateTime.parse(to, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"))

"${formatter.format(fromDate)} - ${formatter.format(toDate)}"
And the import qualifiers are all from this package
kotlinx.datetime.internal.JSJoda.*
r

Raed Ghazal

11/24/2023, 10:42 AM
ah interesting, then I’m sorry but can’t really help you as I have zero experience in java script 😅
c

Christiano

11/24/2023, 10:43 AM
Not a problem, that's my issue as well xD I'll try some other things and see what I can do, maybe ask a question outside of this thread. I any case, thanks for your replies and the article!
🙌 1
1
Ah quick update... I found a solution 😅 By using the regular
kotlinx.datetime.*
import qualifier I noticed there where other functions I could use, by using this I can actually change the format of the string dependent on the Locale
Copy code
val localeOptions = dateLocaleOptions {
    month = "short"
    year = "numeric"
}

val fromDateFormatted = LocalDateTime.parse(from).toInstant(TimeZone.UTC).toJSDate().toLocaleString("en", localeOptions)
val toDateFormatted = LocalDateTime.parse(to).toInstant(TimeZone.UTC).toJSDate().toLocaleString("en", localeOptions)

"$fromDateFormatted - $toDateFormatted"
When not adding "en" as a Locale, the browser/device Locale seems to be used.
r

Raed Ghazal

11/24/2023, 11:10 AM
cool! good for you 🤝
any case, thanks for your replies and the article!
sure, glad to help, a follow on medium and maybe some claps can help me as well 😄
c

Christiano

11/24/2023, 11:11 AM
Consider it done!
r

Raed Ghazal

11/24/2023, 11:12 AM
Thank you!
2 Views