setYearMonth

fun setYearMonth(yearMonth: YearMonth)(source)

Writes the contents of the specified yearMonth to this DateTimeComponents. The yearMonth is written to the year and monthNumber fields.

If any of the fields are already set, they will be overwritten.

Samples

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

fun main() { 
   //sampleStart 
   // Formatting and parsing a year-month in complex scenarios
val format = DateTimeComponents.Format {
    year(); char('-'); monthNumber()
}
val formattedYearMonth = format.format {
    setYearMonth(YearMonth(2023, Month.FEBRUARY))
    check(year == 2023)
    check(month == Month.FEBRUARY)
}
check(formattedYearMonth == "2023-02")
val parsedYearMonth = format.parse("2023-02")
check(parsedYearMonth.toYearMonth() == YearMonth(2023, Month.FEBRUARY))
check(parsedYearMonth.year == 2023)
check(parsedYearMonth.month == Month.FEBRUARY) 
   //sampleEnd
}