Function to add more hours or days to a date

13

I do not know much about the new Date() function and I wanted a way to add days / hours to a date.

Example:

I have:

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

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

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

If it is in the case of the month, it adds 3 and if it does reach the end of the year it already places as 2015.

I was in need of this code, but I do not know how to do it (I think I should mess around with new Date() ). I count on your help!

    
asked by anonymous 14.03.2014 / 06:01

3 answers

16

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
    
14.03.2014 / 06:09
11

Just to complement: Depending on the project, it may be a good idea to use the Moment.js library that takes good care of this situation.

You can do things like this here:

// Criar uma data e adicionar duas horas
var minhaData = moment(
    "14/03/2014 23:54", "D/M/YYYY h:m"
).add(
    'hours', 2
);

// Retornar string "calendário" humanizada
minhaData.calendar(); // "Tomorrow at 1:54 AM" (também dá para fazer em português)

// Retornar objeto Date
minhaData.toDate(); // Sat Mar 15 2014 01:54:00 GMT-0300 (BRT)
    
14.03.2014 / 11:32
9

Responding to your question itself "Function to add hours or days to a Date":

You want a function to do this for you, well, it's an easy task, as you can see in @CiganoMorrisonMendez's answer , you may not need a function, however, since you > A function, you can link one in the Prototype of the object itself Date , which would state the following:

Date.prototype.addHoras = function(horas){
    this.setHours(this.getHours() + horas)
};
Date.prototype.addMinutos = function(minutos){
    this.setMinutes(this.getMinutes() + minutos)
};
Date.prototype.addSegundos = function(segundos){
    this.setSeconds(this.getSeconds() + segundos)
};
Date.prototype.addDias = function(dias){
    this.setDate(this.getDate() + dias)
};
Date.prototype.addMeses = function(meses){
    this.setMonth(this.getMonth() + meses)
};
Date.prototype.addAnos = function(anos){
    this.setYear(this.getFullYear() + anos)
};
Note that in addition to what you asked for (hours or days) I also added functionality for hours, minutes, seconds, days, months, and years.

After the prototype's declaration you can use them as follows:

//Criando uma data sem parâmetros (tempo atual)
var dt = new Date();
//Exemplo adicionando 1 hora na sua data
dt.addHora(1);
//Exemplo adicionando 30 minutos na sua data
dt.addMinutos(30);
//Exemplo adicionando 15 segundos na sua data
dt.addSegundos(15);
//Exemplo adicionando 10 dias na sua data
dt.addDias(10);
//Exemplo adicionando 2 meses na sua data:
dt.addMeses(2);
//Exemplo adicionando 1 ano na sua data:
dt.addAnos(1);
//Imprimindo ela no console
console.log(dt);

In this way you can add hours / minutes / seconds / days / months / years to your date easily.

    
14.03.2014 / 12:45