Modify the project
Let's modify the code generated by the Kotlin Multiplatform wizard and display the current date within the App composable. To do this, you'll add a new dependency to the project, enhance the UI, and rerun the application on each platform.
Add a new dependency
You could retrieve the date using platform-specific libraries and expected and actual declarations. But we recommend that you use this approach only when there's no Kotlin Multiplatform library available. In this case, you can rely on the kotlinx-datetime library.
To use the kotlinx-datetime library:
Open the
gradle/libs.versions.tomlfile and add thekotlinx-datetimedependency to the version catalog:[versions] kotlinx-datetime = "0.8.0" [libraries] kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datetime" }Open the
shared/build.gradle.ktsfile and add a reference to that library entry to the section that configures the common code source set:kotlin { // ... sourceSets { // ... commonMain.dependencies { // ... implementation(libs.kotlinx.datetime) } } }For the web target, timezone support requires the
js-jodalibrary. Add a reference to thejs-jodanpm package to thewebApp/build.gradle.kts file:kotlin { // ... sourceSets { // ... commonMain.dependencies { // ... implementation(npm("@js-joda/timezone", "2.25.1")) } } }Adding the dependency to the
webMainsource set makes the library available both to thewasmJsandjstargets.Once the dependency is added, accept the IDE suggestion to sync the Gradle configuration or press double Shift and execute the Sync Project with Gradle Files command.
In the Terminal tool window, run the following command to ensure that the
yarn.lockfile is updated with the latest dependency versions:./gradlew kotlinUpgradeYarnLock kotlinWasmUpgradeYarnLockIn the
webApp/src/webMain/kotlin/.../main.ktfile, use the@JsModuleannotation to import thejs-jodanpm package:import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.window.ComposeViewport import kotlin.js.ExperimentalWasmJsInterop import kotlin.js.JsModule @OptIn(ExperimentalWasmJsInterop::class) @JsModule("@js-joda/timezone") external object JsJodaTimeZoneModule private val jsJodaTz = JsJodaTimeZoneModule @OptIn(ExperimentalComposeUiApi::class) fun main() { ComposeViewport { App() } }
Enhance the user interface
Open the
shared/src/commonMain/kotlin/App.ktfile and after theApp()composable add the following function which returns a string containing the current date:fun todaysDate(): String { fun LocalDateTime.format() = toString().substringBefore('T') val now = Clock.System.now() val zone = TimeZone.currentSystemDefault() return now.toLocalDateTime(zone).format() }Add the imports that are suggested by the IDE.
Make sure to import the
Clockclass fromkotlin.time, notkotlinx.datetime.In the same file, modify the
App()composable to include theText()composable that invokes this function and displays the result:@Composable @Preview fun App() { MaterialTheme { var showContent by remember { mutableStateOf(false) } val greeting = remember { Greeting().greet() } Column( modifier = Modifier .safeContentPadding() .fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally ) { Text( text = "Today's date is ${todaysDate()}", modifier = Modifier.padding(20.dp), fontSize = 24.sp, textAlign = TextAlign.Center ) Button(onClick = { showContent = !showContent }) { Text("Click me!") } AnimatedVisibility(showContent) { Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) { Image(painterResource(Res.drawable.compose_multiplatform), null) Text("Compose: $greeting") } } } } }Follow the IDE's suggestions to import the missing dependencies.

Rerun the application
You can now rerun the application using the same run configurations for Android, iOS, desktop, and web:



Next step
In the next part of the tutorial, you'll learn new Compose Multiplatform concepts and create your own application from scratch.
Get help
Kotlin Slack. Get an invite and join the #multiplatform channel.
Kotlin issue tracker. Report a new issue.