Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add 10 seconds to a Date

Tags:

javascript

How can I add 10 seconds to a JavaScript date object?

Something like this:

var timeObject = new Date()      var seconds = timeObject.getSeconds() + 10; timeObject = timeObject + seconds; 
like image 614
george Avatar asked Oct 07 '11 13:10

george


People also ask

How do you add seconds to a date?

Add x Seconds to a Date We can add x number of seconds to a Date instance by using the getSeconds method to get the number of seconds in the date object. For instance, we can write: const date = new Date('2020-01-01 10:11:55'); date.

How can I add 1 day to current date?

const date = new Date(); date. setDate(date. getDate() + 1); // ✅ 1 Day added console.

How do you add 1 day in milliseconds?

There are 60 seconds in a minute, 60 minutes in an hour, and 24 hours in a day. Therefore, one day is: 1000 * 60 * 60 * 24 , which is 86400000 milliseconds. Date.

What is getTime in JavaScript?

getTime() The getTime() method returns the number of milliseconds since the ECMAScript epoch. You can use this method to help assign a date and time to another Date object.


1 Answers

There's a setSeconds method as well:

var t = new Date(); t.setSeconds(t.getSeconds() + 10); 

For a list of the other Date functions, you should check out MDN


setSeconds will correctly handle wrap-around cases:

var d;  d = new Date('2014-01-01 10:11:55');  alert(d.getMinutes() + ':' + d.getSeconds()); //11:55  d.setSeconds(d.getSeconds() + 10);  alert(d.getMinutes() + ':0' + d.getSeconds()); //12:05
like image 147
zzzzBov Avatar answered Oct 15 '22 17:10

zzzzBov