Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!