🛠️ToolsShed

Menstrual Cycle Calculator

Predict your next period and fertile window based on cycle length and last period date.

Perguntas Frequentes

Implementação de Código

from datetime import date, timedelta

def menstrual_cycle(last_period: date, cycle_length: int = 28, period_length: int = 5) -> dict:
    """
    Predict next period and fertile window based on cycle data.
    Ovulation assumed at cycle_length - 14 days (luteal phase = 14 days).
    """
    ovulation_day = cycle_length - 14
    next_period = last_period + timedelta(days=cycle_length)
    ovulation_date = last_period + timedelta(days=ovulation_day)
    fertile_start = ovulation_date - timedelta(days=5)
    fertile_end = ovulation_date

    return {
        "last_period":    last_period.strftime("%Y-%m-%d"),
        "next_period":    next_period.strftime("%Y-%m-%d"),
        "ovulation_date": ovulation_date.strftime("%Y-%m-%d"),
        "fertile_window": f"{fertile_start.strftime('%Y-%m-%d')} to {fertile_end.strftime('%Y-%m-%d')}",
        "period_ends":    (last_period + timedelta(days=period_length - 1)).strftime("%Y-%m-%d"),
    }

result = menstrual_cycle(date(2025, 3, 1), cycle_length=28, period_length=5)
print(f"Last Period    : {result['last_period']}")
print(f"Period Ends    : {result['period_ends']}")
print(f"Fertile Window : {result['fertile_window']}")
print(f"Ovulation      : {result['ovulation_date']}")
print(f"Next Period    : {result['next_period']}")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.