clamp

Returns the receiver limited to an inclusive [min, max] month range: min if the receiver is earlier than min, max if later than max, otherwise the receiver.

import { Month } from "@teakit/month";

Month.from("2024-06").clamp("2025-01", "2025-12").toString(); // "2025-01"
Month.from("2026-03").clamp("2025-01", "2025-12").toString(); // "2025-12"
Month.from("2025-07").clamp("2025-01", "2025-12").toString(); // "2025-07"

// Bounds can be any MonthValue.
Month.from("2026-03").clamp(Month.from("2025-01"), Month.from("2025-12")).toString(); // "2025-12"

API Reference

Signature

month.clamp(min: MonthValue, max: MonthValue): MonthInstance;

Parameters

ParameterTypeRequiredNotes
minMonthValueYesInclusive lower bound.
maxMonthValueYesInclusive upper bound.

Returns

Returns a Month: min if the receiver is below range, max if above range, otherwise the receiver. The returned month preserves the day anchor of whichever value is chosen.

Throws

  • Throws if min is later than max.

Agent Contract

FieldValue
Kindinstance method
Canonical nameclamp
Mutates receiverNo
ReturnsMonthInstance
Related methodsmin, max, cmp

Agent Notes

  • Prefer clamp() over manual lt/gt branching when bounding a single month into a range.
  • clamp() compares by month index only; day anchors never affect the result.
  • Month.min(...) / Month.max(...) are the static helpers for the extremes of a set; clamp() bounds one value into a range.