Date Period
Constructs a new DatePeriod.
It is always recommended to name the arguments explicitly when constructing this manually, like DatePeriod(years = 1, months = 12, days = 16)
.
The passed numbers are not stored as is but are normalized instead for human readability, so, for example, DateTimePeriod(months = 24, days = 41)
becomes DateTimePeriod(years = 2, days = 41)
.
If only a single component is set and is always non-zero and is semantically a fixed time interval (like "yearly" or "quarterly"), please consider using a multiple of DateTimeUnit.DateBased instead. For example, instead of DatePeriod(months = 6)
, one can use DateTimeUnit.MONTH * 6
.
Throws
if the total number of months in years and months overflows an Int.
Samples
import kotlinx.datetime.*
import kotlin.test.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.minutes
fun main() {
//sampleStart
// Constructing a DatePeriod using its constructor
val datePeriod = DatePeriod(years = 1, months = 16, days = 60)
check(datePeriod.years == 2) // 1 year + (16 months / 12)
check(datePeriod.months == 4) // 16 months % 12
check(datePeriod.days == 60)
// the time components are always zero:
check(datePeriod.hours == 0)
check(datePeriod.minutes == 0)
check(datePeriod.seconds == 0)
check(datePeriod.nanoseconds == 0)
//sampleEnd
}