Skip to main content
@spa-tools/utilities

addSeconds()

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

Usage

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

const date = new Date('2021-01-01');
console.log(date.toLocaleDateString('en-US', { hour: 'numeric', minute: 'numeric', second: 'numeric' }));
// --> '01/01/2021 12:00:00 AM'

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

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

const newDate3 = addSeconds(date, -60);
console.log(newDate3.toLocaleDateString('en-US', { hour: 'numeric', minute: 'numeric', second: 'numeric' }));
// --> '12/31/2020 11:59:00 PM'

Arguments

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

Returns

Date - The new date object with the added seconds.