.toInstant()

leoclarkribeiro's avatar
Published Nov 24, 2023
Contribute to Docs

The .toInstant() method is a simple way to convert a date-time string into a Kotlin readable format. The conversion results in obtaining an Instant value, which is a standardized representation of time independent of time zones. Essentially, it helps transform a textual representation of a moment in time into a format that can be more easily manipulated and compared programmatically.

Syntax

fun String.toInstant(): Instant

Example

import kotlinx.datetime.toInstant
fun main() {
val iso8601String = "2023-11-13T11:04:44+00:00"
val instant = iso8601String.toInstant()
println(instant)
}

The above code gives the following output:

2023-11-13T11:04:44Z

All contributors

Contribute to Docs

Learn Kotlin on Codecademy