To add any number of seconds to an already instantiated Date
object, you can simply add them to the seconds from that Date
object. This would give you the total number of seconds, which you can then set on the Date
object using the Date.prototype.setSeconds()
method.
For example, to add 60
seconds to a JavaScript Date
object that's instantiated to 'July 16, 2021 00:00:30'
, you could do the following:
const dt = new Date('July 16, 2021 00:00:30');
const secsToAdd = 60;
const totalSecs = dt.getSeconds() + secsToAdd;
dt.setSeconds(totalSecs);
console.log(dt); // 'Fri Jul 16 2021 00:01:30 GMT+0200 (Central European Summer Time)'
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.