João Henrique de Souza Silva
03/24/2025, 2:46 PMfun LocalDate.toFormattedDateString(): String =
format(LocalDate.Format {
dayOfWeek(DayOfWeekNames.ENGLISH_ABBREVIATED)
chars(", ")
dayOfMonth()
char(' ')
monthName(MonthNames.ENGLISH_ABBREVIATED)
char(' ')
year()
})
How can I make dayOfWeek
and monthName
be translated based on the device language? I'm using kotlinx-datetime.youssef hachicha
03/24/2025, 3:02 PMJoão Henrique de Souza Silva
03/24/2025, 3:05 PMyoussef hachicha
03/24/2025, 3:11 PMyoussef hachicha
03/24/2025, 3:12 PMkotlinx-datetime
library does not support locale-aware formatting for date components like day names and month names. The formatting functions provided by kotlinx-datetime
are locale-invariant, meaning they do not adapt to different device languages or regional settings. This limitation is acknowledged in the library's discussions, where it's noted that a good date-time formatting API should make it easy to write correct code for both machine-human and machine-machine communication, which typically involves locales.João Henrique de Souza Silva
03/24/2025, 3:35 PMyoussef hachicha
03/24/2025, 3:39 PMyoussef hachicha
03/24/2025, 3:41 PMJoão Henrique de Souza Silva
03/24/2025, 3:46 PMJoão Henrique de Souza Silva
03/24/2025, 3:47 PMLucas Prioste
03/24/2025, 5:29 PMexpect fun LocalDate.formatDateToLocale(): String
In android side
actual fun LocalDate.formatDateToLocale(): String {
val formatter = DateTimeFormatter
.ofLocalizedDate(FormatStyle.MEDIUM)
.withLocale(Locale.getDefault())
return this.toJavaLocalDate().format(formatter)
}
In iOS side
actual fun LocalDate.formatDateToLocale(): String {
val formatter = NSDateFormatter().apply {
dateStyle = NSDateFormatterMediumStyle
timeStyle = NSDateFormatterNoStyle
locale = NSLocale.currentLocale
}
val calendar = NSCalendar.currentCalendar
val nsDate = calendar.dateFromComponents(this.toNSDateComponents()) ?: return ""
return formatter.stringFromDate(nsDate)
}
João Henrique de Souza Silva
03/25/2025, 11:43 AMexpect
and actual
to implement it on both platforms.
enum class FormatStyle {
FULL,
LONG,
MEDIUM,
SHORT,
}
import com.my.path.FormatStyle
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.toJavaLocalDate
import java.time.format.DateTimeFormatter
import java.util.Locale
import java.time.format.FormatStyle as FS
actual fun LocalDate.formatDateToLocale(style: FormatStyle): String {
val style = when (style) {
FormatStyle.FULL -> FS.FULL
FormatStyle.LONG -> FS.LONG
FormatStyle.MEDIUM -> FS.MEDIUM
FormatStyle.SHORT -> FS.SHORT
}
val formatter = DateTimeFormatter
.ofLocalizedDate(style)
.withLocale(Locale.getDefault())
return this.toJavaLocalDate().format(formatter)
}
actual fun LocalDate.formatDateToLocale(pattern: String): String {
val formatter = DateTimeFormatter
.ofPattern(pattern)
return this.toJavaLocalDate().format(formatter)
}
actual fun LocalDateTime.formatDateToLocale(style: FormatStyle): String =
date.formatDateToLocale(style)
actual fun LocalDate.formatDateToLocale(style: FormatStyle): String {
val style = when (style) {
FormatStyle.FULL -> NSDateFormatterFullStyle
FormatStyle.LONG -> NSDateFormatterLongStyle
FormatStyle.MEDIUM -> NSDateFormatterMediumStyle
FormatStyle.SHORT -> NSDateFormatterShortStyle
}
val formatter = NSDateFormatter().apply {
dateStyle = style
timeStyle = NSDateFormatterNoStyle
locale = NSLocale.currentLocale
}
val calendar = NSCalendar.currentCalendar
val nsDate = calendar.dateFromComponents(this.toNSDateComponents()) ?: return ""
return formatter.stringFromDate(nsDate)
}
actual fun LocalDate.formatDateToLocale(pattern: String): String {
val formatter = NSDateFormatter().apply {
dateFormat = pattern
locale = NSLocale.currentLocale
}
val calendar = NSCalendar.currentCalendar
val nsDate = calendar.dateFromComponents(this.toNSDateComponents()) ?: return ""
return formatter.stringFromDate(nsDate)
}
actual fun LocalDateTime.formatDateToLocale(style: FormatStyle): String =
date.formatDateToLocale(style)