How to Add Minutes to a Date in TypeScript?

While working on a feature to send timed reminders, I needed to add a few minutes to a given date in my TypeScript app. For this, I first tried using the built-in setMinutes method, but soon realized it could lead to issues when reusing the original date object. I explored other approaches to avoid this and handle time zones properly.

In this tutorial, we’ll learn how to add minutes to a date in TypeScript using native methods and the date-fns library to ensure accuracy.

Why Date Manipulation Matters in TypeScript

Date manipulation is a common requirement in many applications, from setting appointment reminders to calculating expiration times for offers. In TypeScript, which provides strong typing and modern JavaScript features, handling dates can be more intuitive and less error-prone.

Check out: Extract Year From Date in TypeScript

Setting Up Your TypeScript Environment

Before diving into date manipulation, ensure you have a TypeScript environment set up. You can initialize a new TypeScript project with the following commands:

mkdir date-manipulation
cd date-manipulation
npm init -y
npm install typescript --save-dev
npx tsc --init

This setup will create a basic TypeScript project. Now, let’s explore how to add minutes to a date.

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

Adding Minutes Using setMinutes Method in TypeScript

The Date object in TypeScript (inherited from JavaScript) provides a method called setMinutes that allows you to set the minutes for a specific date. Here’s how you can use it:

Example: Scheduling a Meeting

Imagine you are scheduling a meeting for a client, John Doe, in New York. The meeting is set to start at 10:00 AM, and you want to add 45 minutes to the start time.

let meetingDate = new Date('2025-11-17T10:00:00');
console.log('Original Meeting Date:', meetingDate);

meetingDate.setMinutes(meetingDate.getMinutes() + 45);
console.log('Updated Meeting Date:', meetingDate);

Output:

Add minutes to date in Typescript

In this example, we create a new Date object for November 17, 2025, at 10:00 AM. Using getMinutes, we retrieve the current minutes, add 45 to them, and then set the new minutes using setMinutes.

Check this out: Get the Month from a Date in TypeScript

Handling Date Overflow in TypeScript

When adding minutes, it’s essential to handle potential overflows. For instance, adding 90 minutes to 10:30 AM should result in 12:00 PM. The Date object automatically handles such overflows.

Example: Adjusting a Flight Departure Time

Consider a scenario where a flight departs from Los Angeles at 10:30 PM. You need to add 90 minutes to account for a delay.

let departureTime = new Date('2025-11-17T22:30:00Z');
console.log('Original Departure Time:', departureTime);

departureTime.setMinutes(departureTime.getMinutes() + 90);
console.log('Updated Departure Time:', departureTime);

Output:

Update date by adding minutes in Typescript

The Date object correctly updates the time to 12:00 AM on the next day.

Creating a Utility Function

For reusability, it’s a good practice to encapsulate the date manipulation logic in a utility function. This function can be used throughout your application.

Example: Utility Function to Add Minutes

function addMinutes(date: Date, minutes: number): Date {
    let newDate = new Date(date.getTime());
    newDate.setMinutes(newDate.getMinutes() + minutes);
    return newDate;
}

// Usage
let originalDate = new Date('2025-11-17T14:00:00');
let newDate = addMinutes(originalDate, 30);
console.log('Original Date:', originalDate);
console.log('New Date:', newDate);

Output:

Typescript add minutes to date value

This function, addMinutes, creates a new Date object to avoid mutating the original date. It then adds the specified minutes and returns the new date.

Check this out: Get Tomorrow’s Date in TypeScript

Working with Time Zones to Add Minutes in TypeScript

When dealing with dates and times, considering time zones is crucial. TypeScript’s Date object operates in the local time zone by default. For applications requiring precise time zone handling, consider using libraries like moment-timezone or date-fns-tz.

Example: Scheduling Across Time Zones

Let’s say you are scheduling a conference call between participants in San Francisco and New York. The call is set for 9:00 AM PST, and you need to adjust the time for EST.

import { addMinutes } from 'date-fns';
import { toZonedTime, fromZonedTime, formatInTimeZone } from 'date-fns-tz';

const sfTime = new Date('2025-11-17T09:00:00-08:00');

const nyTime = toZonedTime(fromZonedTime(sfTime, 'America/Los_Angeles'), 'America/New_York');

console.log('San Francisco Time:', formatInTimeZone(sfTime, 'America/Los_Angeles', 'yyyy-MM-dd HH:mm:ssXXX'));
console.log('New York Time:', formatInTimeZone(nyTime, 'America/New_York', 'yyyy-MM-dd HH:mm:ssXXX'));

Output:

TypeScript function to add minutes to date

In this example, we use date-fns-tz to handle time zone conversions. The utcToZonedTime and zonedTimeToUtc functions allow precise control over time zones.

Conclusion

Working with dates in TypeScript helps us build better apps. Using the Date object and some useful libraries, we can handle scheduling, time zones, and other date-related tasks more easily.

In this tutorial, we learned how to add minutes to a date, deal with time overflows, write reusable functions, and manage time zones. These steps will help us create apps that work well with dates and times.

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.