Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
JS create a new date based on different interval types

I am trying to make JavaScript that creates a new date based on a certain interval that could be either years, months, days or hours. I would like to create a new date with time depending on interval type. I don't really know how to start.

    var startDate = 2022-04-19T10:00:00.000Z
    var interval = 10
    var intervalType = "days" // could be Months, Days, Hours
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can try this:

var startDate = "2022-04-19T10:00:00.000Z";
function parseISOString(s) {
  var b = s.split(/\D+/);
  return new Date(Date.UTC(b[0], --b[1], b[2], b[3], b[4], b[5], b[6]));
}
function addMonths(date, months) {
    var d = date.getDate();
    date.setMonth(date.getMonth() + +months);
    if (date.getDate() != d) {
      date.setDate(0);
    }
    return date;
}
function addDays(date, days) {
  var result = new Date(date);
  result.setDate(result.getDate() + days);
  return result;
}
const date = parseISOString(startDate).getTime();
var interval = 10;
var intervalType = "days" // could be Months, Days, Hours
const newDate = (function () {
   if (intervalType == "days") {
      return addDays(date, interval).toISOString();
   } else if (intervalType == "months") {
      return addMonths(new Date(date), interval).toISOString();
   } else {
      return new Date((interval * 3600000) + date).toISOString();
   }
})()
console.log(newDate);
about 4 years ago · Juan Pablo Isaza Relatório

0

From your explanation above, you want to be able to set interval and interval type and then generate date distance from start date.

var startDate = "2022-04-19T10:00:00.000Z";
var interval = 5;
var newDate = new Date(startDate);
var intervalType = "hours"; // could be Months, Days, Hours

function generateInterval() {
  if (intervalType === "hours") {
    //if your GMT is +1 then you have to remove 1 from the hours unless your startDate is not constant
    const hour = new Date(startDate).getHours();
    const date = newDate.setHours(hour + interval);
    console.log(new Date(date));
  } else if (intervalType === "days") {
    const day = new Date(startDate).getDate();
    const date = newDate.setDate(day + interval);
    console.log(new Date(date));
  } else if (intervalType === "months") {
    const month = new Date(startDate).getMonth();
    const date = newDate.setMonth(month + interval);
    console.log(new Date(date));
  }
}

generateInterval()

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda