previousOrSame

Returns a LocalDate that has the given dayOfWeek not later than this date.

If the dayOfWeek is already the one this date has, the date will be returned unchanged. See previous for a version that always returns a different date earlier than the provided one.

See also

for a symmetrical operation that searches the given dayOfWeek in the future.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.random.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val today = LocalDate(2026, 2, 9)
check(today.dayOfWeek == DayOfWeek.MONDAY)
// Find the beginning of the Monday-based week that contains the given date.
val startOfCurrentMondayBasedWeek = today.previousOrSame(DayOfWeek.MONDAY)
check(startOfCurrentMondayBasedWeek == LocalDate(2026, 2, 9))
// Find the beginning of the Sunday-based week that contains the given date.
val startOfCurrentSundayBasedWeek = today.previousOrSame(DayOfWeek.SUNDAY)
check(startOfCurrentSundayBasedWeek == LocalDate(2026, 2, 8)) 
   //sampleEnd
}