javascript – Function to add more hours or days to a date

Question:

I don't know much about the new Date() and wanted a way to add days/hours to a date.

Example:

I've:

var Time = "14/03/2014 23:54";

and I want to add 2 hours to Time , so it adds the 2 hours and adds 1 to the day.

var Time = "15/03/2014 01:54";

If in the case of the month, it adds 3 and if it reaches the end of the year, it already puts it as 2015.

I was in need of this code urgently, but I don't know how to do it (I think it must mess with new Date() ). I count on your help!

Answer:

So:

hours

var time = new Date('2014-03-14T23:54:00');
var outraData = new Date();
outraData.setHours(time.getHours() + 2); // Adiciona 2 horas

Days

var time = new Date('2014-03-14T23:54:00');
var outraData = new Date();
outraData.setDate(time.getDate() + 3); // Adiciona 3 dias
Scroll to Top