MMonthAnchor
Workflows

Coding workflows · Clamped calendar month shifts

03
Built around your work

A concrete starting point

Shift a date by whole calendar months and clamp its day to the target month's final day. Check January 31 in leap and non-leap years plus a negative shift, rejecting fractional offsets and years outside the supported range.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Clamped calendar month shifts

A concrete starting point
from calendar import monthrange
from datetime import date

def add_months(day, delta):
    if type(day) is not date or type(delta) is not int:
        raise ValueError("month.input")
    year, month0 = divmod(day.year * 12 + day.month - 1 + delta, 12)
    if not 1 <= year <= 9999:
        raise ValueError("month.year")
    last = monthrange(year, month0 + 1)[1]
    return date(year, month0 + 1, min(day.day, last))

The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.

AI coding workspace

Your next change starts here.

Get the Windows app ↗