ISO

ISO 8601 extended format.

Examples of datetime in ISO 8601 format:

  • 2020-08-30T18:43

  • +12020-08-30T18:43:00

  • 0000-08-30T18:43:00.5

  • -0001-08-30T18:43:00.123456789

When formatting, seconds are always included, even if they are zero. Fractional parts of the second are included if non-zero.

Guaranteed to parse all strings that LocalDateTime.toString produces.

See ISO-8601-1:2019, 5.4.2.1b), the version without the offset, together with LocalDate.Formats.ISO and LocalTime.Formats.ISO.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Parsing and formatting LocalDateTime values using the ISO format
val dateTime1 = LocalDate(2024, 2, 15)
    .atTime(hour = 8, minute = 30, second = 15, nanosecond = 160_000_000)
val dateTime2 = LocalDate(2024, 2, 15)
    .atTime(hour = 8, minute = 30, second = 15)
val dateTime3 = LocalDate(2024, 2, 15)
    .atTime(hour = 8, minute = 30)
check(LocalDateTime.Formats.ISO.parse("2024-02-15T08:30:15.16") == dateTime1)
check(LocalDateTime.Formats.ISO.parse("2024-02-15T08:30:15") == dateTime2)
check(LocalDateTime.Formats.ISO.parse("2024-02-15T08:30") == dateTime3)
check(LocalDateTime.Formats.ISO.format(dateTime1) == "2024-02-15T08:30:15.16")
check(LocalDateTime.Formats.ISO.format(dateTime2) == "2024-02-15T08:30:15")
check(LocalDateTime.Formats.ISO.format(dateTime3) == "2024-02-15T08:30:00") 
   //sampleEnd
}