DEV Community

Cover image for Building a Precision Gestation Timeline Generator in TypeScript
kandz
kandz

Posted on Originally published at tools.kandz.me

Building a Precision Gestation Timeline Generator in TypeScript

In the world of niche SaaS tools, "Calculators" are high-conversion features. One interesting domain is veterinary techβ€”specifically, pregnancy tracking for dogs. While the math is linear, the domain logic requires handling specific windows of time for veterinary diagnostics.

In this post, we’ll build a robust GestationCalculator class in TypeScript that handles the 63-day canine pregnancy cycle.

The Domain Logic

Dog pregnancy is short (avg. 63 days). For a developer, the challenge isn't just adding days, but structuring the data so a UI can represent different "phases" (Confirmation, Calcification, Whelping).

The Implementation

We will create a class that takes a breeding date and returns a structured timeline object. This approach makes it easy to plug into a React or Vue frontend.

type Milestone = {
  label: string;
  startDate: Date;
  endDate?: Date;
  description: string;
};

class CanineGestationCalculator {
  private breedingDate: Date;
  private readonly GESTATION_DAYS = 63;

  constructor(dateInput: string | Date) {
    this.breedingDate = new Date(dateInput);
  }

  private addDays(days: number): Date {
    const newDate = new Date(this.breedingDate);
    newDate.setDate(newDate.getDate() + days);
    return newDate;
  }

  public getTimeline(): Milestone[] {
    return [
      {
        label: "Confirmation Window (Ultrasound)",
        startDate: this.addDays(25),
        endDate: this.addDays(35),
        description: "Best time to visualize gestational sacs and heartbeats."
      },
      {
        label: "Skeleton Ossification (X-Ray)",
        startDate: this.addDays(45),
        endDate: this.addDays(55),
        description: "Fetal skeletons calcify; accurate puppy counts are possible now."
      },
      {
        label: "Nesting Phase",
        startDate: this.addDays(58),
        description: "Prepare the whelping box and start monitoring temperature."
      },
      {
        label: "Expected Due Date",
        startDate: this.addDays(this.GESTATION_DAYS),
        description: "Standard 63-day whelping target."
      }
    ];
  }
}

// Usage Example
const calculator = new CanineGestationCalculator("2024-05-20");
const timeline = calculator.getTimeline();

timeline.forEach(m => {
  console.log(`${m.label}: ${m.startDate.toDateString()}`);
});
Enter fullscreen mode Exit fullscreen mode

Why TypeScript for this?

Using an object-oriented approach with TypeScript ensures that:

  1. Date Integrity: We ensure the input is always parsed into a valid Date object.
  2. Strict Milestones: The Milestone type prevents UI bugs where a description or date might be missing during rendering.
  3. Extensibility: If you need to add "Progesterone testing" logic (which shifts the 63-day anchor), you can easily extend this class.

Key Edge Cases to Consider

  • Timezones: When calculating pregnancy dates, always handle the time as midnight (UTC or local) to avoid off-by-one errors when the user is near the 23:59 mark.
  • Input Validation: Ensure the breeding date isn't in the future (unless you're building a "planning" tool).

Practical Implementation

If you need to see how this logic looks in a production environment with a full UI and PDF export capabilities, check out our Dog Pregnancy Due Date Calendar. It’s a real-world implementation of the timeline logic discussed above.

Top comments (0)