0

Consider I have a date, for example 01 march 2021 and the today date. What I would like to obtain is a new date that is the nearest to today and is 01 march 2021 + n * (14 days) with n a positive number.

Example:

if today is 20 may 2021 ---> 10 may 2021 (so n in that case is 5)
if today is 24 may 2021 ---> 24 may 2021 (n = 6)
if today is 25 may 2021 ---> 24 may 2021 (n = 6)

How can I do that?

I would like to create a function similar to:

function getDateFrom(startDate, stepInDays) {
  return date
}


const startDate = new Date(2021, 2, 1) // 01 march 2021
const d = getDateFrom(startDate, 14)

I'm missing the logic behind that

whitecircle
  • 237
  • 9
  • 34
  • Probably a duplicate of [*How can I add 1 day to current date?*](https://stackoverflow.com/questions/9989382/how-can-i-add-1-day-to-current-date) – RobG May 20 '21 at 21:50
  • @RobG thank you but it's different. I have not only to add `x` days from a date but also get the nearest date from today that dists 14 days from the starting date – whitecircle May 21 '21 at 05:56
  • 1
    It shows how to add *n* days to a date. What you want to do is add *n x 14* so that the new date is 7 days or less from *today*. There are plenty of other question on how to get the different between two dates in days, which will help you get a starting value for *n*. – RobG May 21 '21 at 08:46
  • should `n` always be rounded down? – Dane Brouwer May 21 '21 at 11:17
  • @DaneBrouwer Yes sorry, I didn't specify it but yes, the result date should not be "in the future" – whitecircle May 21 '21 at 11:48

2 Answers2

0

I solved in this way:

const MS_PER_DAY = 1000 * 60 * 60 * 24;
const stepDays = 14;
const startDate = new Date(2021, 2, 1);
console.log("startDate:", startDate);

const todayDate = new Date();
console.log("todayDate:", todayDate);

const diffTime = Math.abs(todayDate - startDate);
const diffDays = Math.ceil(diffTime / MS_PER_DAY);
console.log("diffDays:", diffDays);

const n = diffDays / stepDays;
const nFloored = Math.floor(n);
console.log("n:", n);

const lastUpdateDate = new Date(startDate.getTime());
lastUpdateDate.setTime(
  lastUpdateDate.getTime() + nFloored * stepDays * MS_PER_DAY
);
console.log("lastUpdateDate:", lastUpdateDate);

I know it's not very correct but for me it's ok.

whitecircle
  • 237
  • 9
  • 34
  • 1
    Days aren't always 24 hours long where daylight saving is observed. *diffDays* will be more reliable if you first set *todayDate* to 00:00:00 using `todayDate.setHours(0,0,0,0)` then use `Math.round(diffTime / MS_PER_DAY)` to remove any daylight saving effect. – RobG May 21 '21 at 08:52
-1

To get into your issue, we have to first write your problem as an equation

01 march 2021 + (n * 14) = current date

(n * 14) = current date - 01 march 2021

n = (current date - 01 march 2021)/14

That gets us n we then need to get the closest past date to n, so we can just use Math.floor to get to the closest past date.

01 march 2021 + (floored n * 14) = closest date

const milisecondsPerDay = 24*60*60*1000;
const dayStep = 14;

const originDate = new Date(2021, 2, 1);
const originDateInSeconds = originDate.getTime();

function getClosestDate(date = new Date()) {
  const dateInSeconds = date.setHours(0,0,0,0);
  const n = (dateInSeconds-originDateInSeconds)/(dayStep*milisecondsPerDay);
  
  console.log(n);
  const flooredN = Math.floor(n);
  console.log(flooredN);
  const closestDate = new Date(originDateInSeconds+(flooredN*dayStep*milisecondsPerDay))
  console.log(closestDate.toString());
  return closestDate;
}
getClosestDate();
Dane Brouwer
  • 2,827
  • 1
  • 22
  • 30