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
composeApp/build.gradle.ktsfile and add the dependencies to the project:Add the main
kotlinx-datetimedependency to the section that configures the common code source set. For simplicity, you can include the version number directly instead of adding it to the version catalog.For the web target, timezone support requires the
js-jodalibrary. Add a reference to thejs-jodanpm package in thewebMaindependencies.
kotlin { // ... sourceSets { // ... commonMain.dependencies { // ... implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.7.1") } webMain.dependencies { implementation(npm("@js-joda/timezone", "2.22.0")) } } }Once the dependency is added, you're prompted to resync the project. Click the Sync Gradle Changes button to synchronize Gradle files:

In the Terminal tool window, run the following command:
./gradlew kotlinUpgradeYarnLock kotlinWasmUpgradeYarnLockThis Gradle task ensures that the
yarn.lockfile is updated with the latest dependency versions.In the
webMainsource set, 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
composeApp/src/commonMain/kotlin/App.ktfile and 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. Make sure to import all the missing dependencies for the
todaysDate()function from the updated packages, and opt in when prompted by the IDE.
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.