HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

How can I add minutes, hours or days to a JavaScript date?

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptdatehowhoursdayscanaddminutes

Problem

You can't get too far in web development without stumbling upon a situation where you have to manipulate dates. Perhaps you need to display a delivery date or a deadline. As easy as this sounds, JavaScript doesn't provide a particularly friendly API for such tasks. But that's nothing that you can't solve with a little ingenuity.
> [!NOTE]
>
> You may not be familiar with JavaScript's numeric separators, which are used in the examples below. They're syntactic sugar that make large numeric values more readable.
The Date object has Date.prototype.getTime() and Date.prototype.setTime() built-in. These two methods allow us to get the current time in a date object and set the time in a date object, respectively.

Solution

const addSecondsToDate = (date, n) => {
  const d = new Date(date);
  d.setTime(d.getTime() + n * 1000);
  return d;
};

addSecondsToDate(new Date('2020-10-19 12:00:00'), 10);
// 2020-10-19 12:00:10
addSecondsToDate(new Date('2020-10-19 12:00:00'), -10);
// 2020-10-19 11:59:50


>
> You may not be familiar with JavaScript's numeric separators, which are used in the examples below. They're syntactic sugar that make large numeric values more readable.
The Date object has Date.prototype.getTime() and Date.prototype.setTime() built-in. These two methods allow us to get the current time in a date object and set the time in a date object, respectively.
All values are in milliseconds, so we'll have to multiply the number of seconds by 1000 before setting the time. Note, however, that setting the time mutates the object, so we'll also have to create a new Date object to avoid mutating the original.
Similarly to seconds, we can add minutes to a date by multiplying the number of milliseconds in a minute (1000 * 60) before setting the time.
To add hours to a date, we can multiply the number of milliseconds in an hour (1000 60 60) before setting the time.

Code Snippets

const addSecondsToDate = (date, n) => {
  const d = new Date(date);
  d.setTime(d.getTime() + n * 1000);
  return d;
};

addSecondsToDate(new Date('2020-10-19 12:00:00'), 10);
// 2020-10-19 12:00:10
addSecondsToDate(new Date('2020-10-19 12:00:00'), -10);
// 2020-10-19 11:59:50
const addMinutesToDate = (date, n) => {
  const d = new Date(date);
  d.setTime(d.getTime() + n * 60_000);
  return d;
};

addMinutesToDate('2020-10-19 12:00:00', 10);
// 2020-10-19 12:10:00
addMinutesToDate('2020-10-19 12:00:00', -10);
// 2020-10-19 11:50:00
const addHoursToDate = (date, n) => {
  const d = new Date(date);
  d.setTime(d.getTime() + n * 3_600_000);
  return d;
};

addHoursToDate('2020-10-19 12:00:00', 10);
// 2020-10-19 22:00:00
addHoursToDate('2020-10-19 12:00:00', -10);
// 2020-10-19 02:00:00

Context

From 30-seconds-of-code: add-minutes-hours-days-to-date

Revisions (0)

No revisions yet.