year Two Digits
The last two digits of the ISO year.
baseYear is the base year for the two-digit year. For example, if baseYear is 1960, then this format correctly works with years 1960..2059.
On formatting, when given a year in the valid range, it returns the last two digits of the year, so 1993 becomes "93". When given a year outside the valid range, it returns the full year number with a leading sign, so 1850 becomes "+1850", and -200 becomes "-200".
On parsing, it accepts either a two-digit year or a full year number with a leading sign. When given a two-digit year, it returns a year in the valid range, so "93" becomes 1993, and when given a full year number with a leading sign, it parses the full year number, so "+1850" becomes 1850.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() {
//sampleStart
// Using two-digit years in a custom format
val format = LocalDate.Format {
yearTwoDigits(baseYear = 1960); char(' '); monthNumber(); char('/'); dayOfMonth()
}
check(format.format(LocalDate(1960, 1, 13)) == "60 01/13")
check(format.format(LocalDate(2000, 1, 13)) == "00 01/13")
check(format.format(LocalDate(2021, 1, 13)) == "21 01/13")
check(format.format(LocalDate(2059, 1, 13)) == "59 01/13")
check(format.format(LocalDate(2060, 1, 13)) == "+2060 01/13")
check(format.format(LocalDate(-13, 1, 13)) == "-13 01/13")
//sampleEnd
}