Skip to main content
@spa-tools/utilities

addHours()

The addHours() function adds a number of hours to a given date object, timestamp integer, or date string.

Usage

import { addHours } from '@spa-tools/utilities';

const date = new Date('2021-01-01');

const newDate1 = addHours(date, 5);
console.log(newDate1.toLocaleDateString('en-US', { hour: 'numeric', minute: 'numeric' }));
// --> '01/01/2021 5:00 AM'

const newDate2 = addHours(date.getTime(), 6);
console.log(newDate2.toLocaleDateString('en-US', { hour: 'numeric', minute: 'numeric' }));
// --> '01/01/2021 6:00 AM'

const newDate3 = addHours('2021-01-01', 7);
console.log(newDate3.toLocaleDateString('en-US', { hour: 'numeric', minute: 'numeric' }));
// --> '01/01/2021 7:00 AM'

Arguments

NameTypeRequired?Description
datestring | number | DateyesThe date value to add hours to as either date string, timestamp integer, or date object
hoursnumberyesThe number of hours to add

Returns

Date - The new date object with the added hours.