toIsoString

Common
JVM
JS
Native
1.0
fun toIsoString(): String
(source)

Returns an ISO-8601 based string representation of this duration.

The returned value is presented in the format PThHmMs.fS, where h, m, s are the integer components of this duration (see toComponents) and f is a fractional part of second. Depending on the roundness of the value the fractional part can be formatted with either 0, 3, 6, or 9 decimal digits.

The infinite duration is represented as "PT9999999999999H" which is larger than any possible finite duration in Kotlin.

Negative durations are indicated with the sign - in the beginning of the returned string, for example, "-PT5M30S".

import kotlin.test.*

import kotlin.time.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds

fun main(args: Array<String>) {
//sampleStart
println(25.nanoseconds.toIsoString()) // PT0.000000025S
println(120.3.milliseconds.toIsoString()) // PT0.120300S
println(30.5.seconds.toIsoString()) // PT30.500S
println(30.5.minutes.toIsoString()) // PT30M30S
println(86420.seconds.toIsoString()) // PT24H0M20S
println(2.days.toIsoString()) // PT48H
println(Duration.ZERO.toIsoString()) // PT0S
println(Duration.INFINITE.toIsoString()) // PT9999999999999H
//sampleEnd
}