Add Months to a Date in TypeScript

Recently, I was working on a financial dashboard project where I needed to calculate payment due dates that were several months in the future. The issue is… TypeScript doesn’t have a built-in “addMonths” method like some other languages do. So we need a workaround. In this tutorial, I’ll cover five simple ways you can use to add months to a date in TypeScript with examples.

Method 1 – Using the Date Object’s setMonth Method

The simplest way to add months to a date in TypeScript is to use the built-in setMonth() method. This method allows you to set the month value for a date, and we can use it to add months as well.

Here is an example.

function addMonths(date: Date, months: number): Date {
    const result = new Date(date);
    result.setMonth(result.getMonth() + months);
    return result;
  }
  
  // Example usage
  const currentDate = new Date('2025-04-25'); // April 25, 2025
  const futureDate = addMonths(currentDate, 3); // April 15, 2025
  console.log(futureDate.toDateString()); // "Fri Apr 25 2025"

This method works by:

  1. Creating a new Date object (to avoid modifying the original)
  2. Adding the specified number of months to the current month
  3. Returning the new date

Here is the exact output in the screenshot below;

Add Months to a Date in TypeScript

One nice feature of this approach is that it handles month transitions automatically. For example, if you add 1 month to January, 31, it correctly gives you February 28 (or 29 in leap years) since February doesn’t have 31 days.

Check out Get the Current Date in TypeScript

Method 2 – Using date-fns Library

If you’re working on a project that already uses date-fns (a popular date utility library), this is probably the cleanest approach.

First, install date-fns:

npm install date-fns

Then you can use the addMonths function:

import { addMonths } from 'date-fns';

// Example usage
const currentDate = new Date('2025-04-25');
const futureDate = addMonths(currentDate, 3);
console.log(futureDate.toDateString());

The date-fns approach is great because:

  1. It’s well-tested and handles edge cases
  2. The API is very clean and readable
  3. It’s immutable (doesn’t modify your original date)

This is my go-to method when working on larger projects that involve frequent date manipulation.

Check out How to Set a Date Value to Null in TypeScript

Method 3 – Using moment.js Library

Moment.js is another popular library for date manipulation, though it’s becoming less recommended for new projects due to its size. If you’re already using moment.js though, it provides a very simple way to add months:

import * as moment from 'moment';

// Example usage
const currentDate = new Date('2023-01-15');
const futureDate = moment(currentDate).add(3, 'months').toDate();
console.log(futureDate.toDateString()); // "Sat Apr 15 2023"

Moment’s method is particularly useful when you need to chain multiple date operations together, like adding both months and days.

Method 4 – Using a Custom Function with Day Preservation

Sometimes you might want to preserve the exact day of the month when possible. For example, if you’re adding 1 month to January 30, you probably want February 28 (or 29 in leap years) rather than March 2. Here’s a more advanced function that handles this:

function addMonthsPreserveDay(date: Date, months: number): Date {
  const result = new Date(date);
  const desiredDay = result.getDate();
  
  result.setMonth(result.getMonth() + months);
  
  // If the day changed, it means we've hit the end of the month
  if (result.getDate() !== desiredDay) {
    // Go back to the last day of the previous month
    result.setDate(0);
  }
  
  return result;
}

// Example: Adding 1 month to January 30, 2023
const startDate = new Date('2023-01-30');
const endDate = addMonthsPreserveDay(startDate, 1);
console.log(endDate.toDateString()); // "Tue Feb 28 2023"

This function is handy for financial applications or subscription services that require preserving the day of the month whenever possible.

Check out Create a Date from Year, Month, and Day in TypeScript

Method 5 – Using Temporal API

The Temporal API is a newer, more powerful replacement for JavaScript’s Date object. While it’s not fully standardized yet, it’s worth knowing about for future projects.

You’ll need to use a polyfill for now:

npm install @js-temporal/polyfill

Then you can use it like this:

import { Temporal } from '@js-temporal/polyfill';

const currentDate = Temporal.PlainDate.from('2025-04-25');
const futureDate = currentDate.add({ months: 3 });
console.log(futureDate.toString());

The Temporal API is great because:

  1. It has a more intuitive API than the built-in Date
  2. It’s immutable by design
  3. It handles time zones properly

While it’s not fully supported in all browsers yet, it’s worth keeping an eye on this API as it represents the future of date handling in JavaScript and TypeScript.

Important Points

When adding months to dates, there are some tricky edge cases to consider:

  1. Month end dates: Adding a month to January 31 could result in March 3 (using simple methods) since February doesn’t have 31 days.
  2. Leap years: February 29 exists only in leap years, so adding one year to February 29, 2020 could result in February 28, 2021 or March 1, 2021 depending on your approach.
  3. Time zones: If working with dates across time zones, be especially careful when adding months near daylight saving time transitions.

In this tutorial, I have explained how to add months to a date in TypeScript using various methods, including libraries such as moment.js and date-fns.

If you have any questions or suggestions, I’d love to hear from you in the comments below!

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.