to String
Converts this datetime value to the ISO 8601 string representation.
For readability, if the time represents a round minute (without seconds or fractional seconds), the string representation will not include seconds. Also, fractions of seconds will add trailing zeros to the fractional part until its length is a multiple of three.
Examples of output:
2020-08-30T18:43
2020-08-30T18:43:00
2020-08-30T18:43:00.500
2020-08-30T18:43:00.123456789
See also
for details of how the time part is formatted.
for a very similar format. The difference is that Formats.ISO will always include seconds, even if they are zero, and will not add trailing zeros to the fractional part of the second for readability.
for the dual operation: obtaining LocalDateTime from a string.
for formatting using a custom format.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() {
//sampleStart
// Converting LocalDateTime values to strings
check(LocalDate(2024, 2, 15).atTime(16, 48).toString() == "2024-02-15T16:48")
check(LocalDate(2024, 2, 15).atTime(16, 48, 15).toString() == "2024-02-15T16:48:15")
check(LocalDate(2024, 2, 15).atTime(16, 48, 15, 120_000_000).toString() == "2024-02-15T16:48:15.120")
//sampleEnd
}