0

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
  • 1
    you can use a 3rd party library, something like luxon https://moment.github.io/luxon/#/ – Bk Santiago Apr 16 '22 at 13:57
  • See [*How to add days to Date?*](https://stackoverflow.com/questions/563406/how-to-add-days-to-date) You can use essentially the same algorithm for adding years, months, days, hours, minutes, seconds and milliseconds. But adding arbitrary periods is not easy as date arithmetic isn't necessarily symmetric and sometimes the order that parts are added is important. – RobG Apr 17 '22 at 11:44

2 Answers2

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);
Tanay
  • 871
  • 1
  • 6
  • 12
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()
  • You can't add months as simplistically as that, see [*JavaScript function to add X months to a date*](https://stackoverflow.com/questions/2706125/javascript-function-to-add-x-months-to-a-date). – RobG Apr 17 '22 at 11:51
  • The approved answer in the link you referenced is similar to my implementation or I'm missing something? – Blessing Ladejobi Apr 18 '22 at 19:37
  • That `"accepted" != "approved"`? Similar, yes, but not the same in regard to adding a month where the resulting month has fewer days than the original month. 31 January + 1 month probably shouldn't end up in March. – RobG Apr 19 '22 at 00:34