month
The month (Month) component of the date.
This is a view of monthNumber. Setting it will set monthNumber, and getting it will return a Month instance if monthNumber is a valid month.
Throws
during getting if monthNumber is outside the 1..12
range.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() {
//sampleStart
// Formatting and parsing a date in complex scenarios
val format = DateTimeComponents.Format {
year(); char('-'); monthNumber(); char('-'); dayOfMonth()
}
val formattedDate = format.format {
setDate(LocalDate(2023, 1, 2))
check(year == 2023)
check(month == Month.JANUARY)
check(dayOfMonth == 2)
check(dayOfWeek == DayOfWeek.MONDAY)
}
check(formattedDate == "2023-01-02")
val parsedDate = format.parse("2023-01-02")
check(parsedDate.toLocalDate() == LocalDate(2023, 1, 2))
check(parsedDate.year == 2023)
check(parsedDate.month == Month.JANUARY)
check(parsedDate.dayOfMonth == 2)
check(parsedDate.dayOfWeek == null)
//sampleEnd
}
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() {
//sampleStart
// Setting the month using the `month` property
val input = "Mon, 30 Jul 2008 11:05:30 GMT"
val parsed = DateTimeComponents.Formats.RFC_1123.parse(input)
check(parsed.monthNumber == 7)
check(parsed.month == Month.JULY)
parsed.month = Month.JUNE
check(parsed.monthNumber == 6)
check(parsed.month == Month.JUNE)
//sampleEnd
}