Hi, Working on formatting date in a project. For A...
# multiplatform
s
Hi, Working on formatting date in a project. For Android and IOS
Copy code
internal expect fun formatDate(value: Long, format: String): String
is straight forward. For JavaScript in Kotlin/JS is there any equivalent for formatting the date.
kotlin.js.Date
has no formatting capabilities.
kotlinx-datetime
also seems to lack formatting capabilities. Am I missing something trivial?
a
You can check this one docs.korge.org/klock/
s
Will try this. Any alternatives to importing a whole library?
m
kotlinx-datetime is actually have some support you can format the instant ie
Copy code
fun Instant.toDateString(
    timeZone: TimeZone = TimeZone.currentSystemDefault()
) = toLocalDateTime(timeZone).run {
    "${hour.doubleDigits()}:${minute.doubleDigits()} " +
        "${dayOfMonth.doubleDigits()}.${monthNumber.doubleDigits()}.${year.doubleDigits()}"
}
where
doubleDigits
is
Copy code
fun Int.doubleDigits() = if (this <= BIGGEST_DIGIT) "0$this" else "$this"