month Number
A month-of-year number, from 1 to 12.
By default, it's padded with zeros to two digits. This can be changed by passing padding.
Samples
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.YearMonth
import kotlinx.datetime.format.DayOfWeekNames
import kotlinx.datetime.format.MonthNames
import kotlinx.datetime.format.Padding
import kotlinx.datetime.format.alternativeParsing
import kotlinx.datetime.format.char
import kotlin.test.Test
fun main() {
//sampleStart
// Using month number with various paddings in a custom format
val zeroPaddedMonths = LocalDate.Format {
monthNumber(); char('/'); dayOfMonth(); char('/'); year()
}
check(zeroPaddedMonths.format(LocalDate(2021, 1, 13)) == "01/13/2021")
check(zeroPaddedMonths.format(LocalDate(2021, 12, 13)) == "12/13/2021")
val spacePaddedMonths = LocalDate.Format {
monthNumber(padding = Padding.SPACE); char('/'); dayOfMonth(); char('/'); year()
}
check(spacePaddedMonths.format(LocalDate(2021, 1, 13)) == " 1/13/2021")
check(spacePaddedMonths.format(LocalDate(2021, 12, 13)) == "12/13/2021")
//sampleEnd
}