parse

expect fun parse(input: CharSequence, format: DateTimeFormat<YearMonth> = Formats.ISO): YearMonth(source)

A shortcut for calling DateTimeFormat.parse.

Parses a string that represents a date and returns the parsed YearMonth value.

If format is not specified, Formats.ISO is used. 2023-01 is an example of a string in this format.

See also

for formatting using the default format.

for formatting using a custom format.

Throws

if the text cannot be parsed or the boundaries of YearMonth are exceeded.

Samples

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

fun main() { 
   //sampleStart 
   // Parsing YearMonth values using predefined and custom formats
check(YearMonth.parse("2024-04") == YearMonth(2024, Month.APRIL))
val customFormat = YearMonth.Format {
    monthName(MonthNames.ENGLISH_ABBREVIATED); chars(", "); year()
}
check(YearMonth.parse("Apr, 2024", customFormat) == YearMonth(2024, Month.APRIL)) 
   //sampleEnd
}