Subtract Hours or Minutes from a Date in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
6 min

banner

# Table of Contents

  1. Subtract Hours from a Date in JavaScript
  2. Subtract Minutes from a Date in JavaScript

If you need to subtract minutes from a Date, click on the second subheading.

# Subtract Hours from a Date in JavaScript

To subtract hours from a date:

  1. Use the getHours() method to get the hours of the specific date.
  2. Use the setHours() method to set the hours for the date.
  3. The setHours method takes the hours as a parameter and sets the value for the date.
index.js
function subtractHours(date, hours) { date.setHours(date.getHours() - hours); return date; } // โœ… Subtract 1 hour from the current date const result1 = subtractHours(new Date(), 1); console.log(result1); // ๐Ÿ‘‰๏ธ 2023-07-27T15:48:05.075Z // โœ… Subtract 2 hours from a different date const date = new Date('2024-03-18T13:30:10.000Z'); const result2 = subtractHours(date, 2); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-03-18T11:30:10.000Z

subtract hours from date

The code for this article is available on GitHub

The subtractHours function takes a Date object and N as parameters and subtracts N hours from the date.

If you need to subtract hours from the current date, call the Date() constructor without passing it any arguments.

index.js
function subtractHours(date, hours) { date.setHours(date.getHours() - hours); return date; } // โœ… Subtract 1 hour from the current date const currentDate = new Date(); const result1 = subtractHours(currentDate, 1); console.log(result1); // ๐Ÿ‘‰๏ธ 2023-07-27T15:49:26.045Z

subtract hours from current date

The getHours() method returns a number between 0 and 23 that represents the hour for the given date according to local time.

index.js
const currentDate = new Date(); console.log(currentDate.getHours()); // ๐Ÿ‘‰๏ธ 14

The setHours() method takes a number representing the hours as a parameter and sets the value for the date.

The JavaScript Date object automatically takes care of adjusting the day of the month, the month and the year, if subtracting X hours from the date pushes us into the previous day, month or year.

index.js
function subtractHours(date, hours) { date.setHours(date.getHours() - hours); return date; } const date = new Date('2024-03-18T13:30:10.000Z'); const result = subtractHours(date, 14); console.log(result); // ๐Ÿ‘‰๏ธ 2024-03-17T23:30:10.000Z

Subtracting 14 hours from the date automatically adjusted the day of the month in the example.

# Subtract Hours from a Date without mutation

Note that the setHours method mutates the Date object it was called on.

If you don't want to change the Date in place, create a copy before calling the method.

index.js
function subtractHours(date, hours) { const dateCopy = new Date(date); dateCopy.setHours(dateCopy.getHours() - hours); return dateCopy; } // โœ… Subtract 2 hours from a different date const date = new Date('2024-03-18T13:30:10.000Z'); const result = subtractHours(date, 5); console.log(result); // ๐Ÿ‘‰๏ธ 2024-03-18T08:30:10.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-03-18T13:30:10.000Z

subtract hours from date without mutation

The code for this article is available on GitHub

When a Date object is passed to the Date() constructor, it gets converted to a timestamp and can be used to create a copy of the date.

Mutating function parameters is a bad practice because calling the function with the same parameter multiple times returns different results.

Instead, pure functions like the one above return the same output when called with the same parameters.

# Subtract Hours from a Date using date-fns

You can also use date-fns module to subtract hours from a date.

index.js
import {subHours} from 'date-fns'; const date = new Date('2024-03-18T13:30:10.000Z'); const result1 = subHours(date, 6); console.log(result1); // ๐Ÿ‘‰๏ธ 2024-03-18T07:30:10.000Z const result2 = subHours(date, 10); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-03-18T03:30:10.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-03-18T13:30:10.000Z

subtract hours from date using date fns

The code for this article is available on GitHub

The subHours() function takes a date and the number of hours to be subtracted from the date as parameters.

The function doesn't mutate the original date as shown in the example.

If you don't have date-fns installed, you can install it by running the following command from your terminal.

shell
# ๐Ÿ‘‡๏ธ create package.json if you don't have one npm init -y # โœ… install with NPM npm install date-fns # โœ… install with YARN yarn add date-fns

# Subtract Hours from a Date using moment.js

You can also use the moment.js module to subtract hours from a date.

index.js
import moment from 'moment'; const date = new Date('2024-03-18T13:30:10.000Z'); const result1 = moment(date).subtract(6, 'hours'); console.log(result1); // ๐Ÿ‘‰๏ธ 2024-03-12T06:30:10.000Z const result2 = moment(date).subtract(10, 'hours'); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-03-08T06:30:10.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-03-18T13:30:10.000Z

subtract hours from date using moment

The code for this article is available on GitHub

We used the moment().subtract() method to subtract hours from a date.

The method can be used to subtract years, quarters, months, weeks, days, hours, minutes, seconds or milliseconds from a date.

If you don't have moment installed, you can install it by running the following command from your terminal.

shell
# ๐Ÿ‘‡๏ธ create package.json if you don't have one npm init -y # โœ… install with NPM npm install moment # โœ… install with YARN yarn add moment

The call to the add() method actually returns a moment object and not a native JavaScript date.

If you need to convert the value to a JavaScript date, use the toDate() method.

index.js
import moment from 'moment'; const date = new Date('2024-03-18T13:30:10.000Z'); const result1 = moment(date).subtract(6, 'hours').toDate(); console.log(result1); // ๐Ÿ‘‰๏ธ 2024-03-12T06:30:10.000Z const result2 = moment(date).subtract(10, 'hours').toDate(); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-03-08T06:30:10.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-03-18T13:30:10.000Z

The toDate() method takes care of converting the moment object to a native JavaScript Date object.

# Subtract Minutes from a Date in JavaScript

To subtract minutes from a date:

  1. Use the getMinutes() method to get the minutes of the given date.
  2. Use the setMinutes() method to set the minutes for the date.
  3. The setMinutes method takes the minutes as a parameter and sets the value for the date.
index.js
function subtractMinutes(date, minutes) { date.setMinutes(date.getMinutes() - minutes); return date; } // โœ… Subtract 1 minute from the current date const result1 = subtractMinutes(new Date(), 1); console.log(result1); // ๐Ÿ‘‰๏ธ 2023-01-13T15:42:37.540Z // โœ… Subtract 10 minutes from a different date const date = new Date('2024-09-15T13:46:15.000Z'); const result2 = subtractMinutes(date, 10); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-09-15T13:36:15.000Z
The code for this article is available on GitHub

The subtractMinutes function takes a Date object and N as parameters and subtracts N minutes from the Date.

If you need to subtract minutes from the current date, call the Date() constructor without passing it any arguments.

index.js
function subtractMinutes(date, minutes) { date.setMinutes(date.getMinutes() - minutes); return date; } // โœ… Subtract 1 minute from the current date const currentDate = new Date(); const result1 = subtractMinutes(currentDate, 1); console.log(result1); // ๐Ÿ‘‰๏ธ 2023-01-13T15:42:37.540Z

The Date.getMinutes() method returns a number between 0 and 59 that represents the minutes in the given date.

index.js
const currentDate = new Date(); console.log(currentDate.getMinutes()); // ๐Ÿ‘‰๏ธ 42

The setMinutes() method takes a number representing the minutes as a parameter and then sets the value on the date.

The JavaScript Date object automatically takes care of adjusting the hours, days, months and years if subtracting X minutes from the date changes their values.

index.js
function subtractMinutes(date, minutes) { date.setMinutes(date.getMinutes() - minutes); return date; } const date = new Date('2024-09-15T13:46:15.000Z'); const result = subtractMinutes(date, 47); console.log(result); // ๐Ÿ‘‰๏ธ 2024-09-15T12:51:15.000Z

Subtracting 47 minutes from the date automatically adjusted the hours in the example.

# Subtract minutes from a Date without mutation

Note that the setMinutes method mutates the Date object it was called on.

If you don't want to change the Date in place, you can create a copy of it before calling the method.

index.js
function subtractMinutes(date, minutes) { const dateCopy = new Date(date); dateCopy.setMinutes(dateCopy.getMinutes() - minutes); return dateCopy; } const date = new Date('2024-09-15T13:46:15.000Z'); const result = subtractMinutes(date, 15); console.log(result); // ๐Ÿ‘‰๏ธ 2024-09-15T13:31:15.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-09-15T13:46:15.000Z
The code for this article is available on GitHub

When a Date object is passed to the Date() constructor, it gets converted to a timestamp and can be used to create a copy of the date.

Mutating function parameters is a bad practice because calling the function with the same parameter multiple times returns different results.

Instead, pure functions like the one above return the same output when called with the same parameters.

# Subtract Minutes from a Date using date-fns

You can also use date-fns module to subtract minutes from a date.

index.js
import {subMinutes} from 'date-fns'; const date = new Date('2024-09-15T13:46:15.000Z'); const result1 = subMinutes(date, 6); console.log(result1); // ๐Ÿ‘‰๏ธ 2024-09-15T13:40:15.000Z const result2 = subMinutes(date, 10); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-09-15T13:36:15.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-09-15T13:46:15.000Z
The code for this article is available on GitHub

The subMinutes() function takes a date and the number of minutes to be subtracted from the date as parameters.

The function doesn't mutate the original date as shown in the example.

If you don't have date-fns installed, you can install it by running the following command from your terminal.

shell
# ๐Ÿ‘‡๏ธ create package.json if you don't have one npm init -y # โœ… install with NPM npm install date-fns # โœ… install with YARN yarn add date-fns

# Subtract Minutes from a Date using moment.js

You can also use the moment.js module to subtract minutes from a date.

index.js
import moment from 'moment'; const date = new Date('2024-09-15T13:46:15.000Z'); const result1 = moment(date).subtract(6, 'minutes'); console.log(result1); // ๐Ÿ‘‰๏ธ 2024-03-12T06:30:10.000Z const result2 = moment(date).subtract(10, 'minutes'); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-03-08T06:30:10.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-09-15T13:46:15.000Z
The code for this article is available on GitHub

We used the moment().subtract() method to subtract minutes from a date.

The method can be used to subtract years, quarters, months, weeks, days, hours, minutes, seconds or milliseconds from a date.

If you don't have moment installed, you can install it by running the following command from your terminal.

shell
# ๐Ÿ‘‡๏ธ create package.json if you don't have one npm init -y # โœ… install with NPM npm install moment # โœ… install with YARN yarn add moment

The call to the add() method actually returns a moment object and not a native JavaScript date.

If you need to convert the value to a JavaScript date, use the toDate() method.

index.js
import moment from 'moment'; const date = new Date('2024-09-15T13:46:15.000Z'); const result1 = moment(date).subtract(6, 'minutes').toDate(); console.log(result1); // ๐Ÿ‘‰๏ธ 2024-03-12T06:30:10.000Z const result2 = moment(date).subtract(10, 'minutes').toDate(); console.log(result2); // ๐Ÿ‘‰๏ธ 2024-03-08T06:30:10.000Z console.log(date); // ๐Ÿ‘‰๏ธ 2024-09-15T13:46:15.000Z
The code for this article is available on GitHub

The toDate() method takes care of converting the moment object to a native JavaScript Date object.

I've also written articles on how to add hours to a Date and how to add minutes to a date.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev