0

I want to use the dayjs JavaScript library or pure JavaScript to calculate next dues date (two weeks) from now and print it out on the screen. What I mean is that, an event is happening today and the next date it will happen is two weeks from today. Whenever the event happens, I will say the next event date is the two weeks' date. I am stuck after the little effort I have made so far with the code below. I will appreciate some help.

let today_event = dayjs();
today_event.format();

function eventDate()
{
console.log(`${today_event}`) //prints out today's date as I test it with this

}

eventDate(); //To call the function

That is what I have done so far trying to create a function for it. Now, I am stuck on how to go further and calculate a future date from the current date. Maybe, I am not even going about it the right way. So, I will appreciate every help.

aye decoder
  • 105
  • 1
  • 2
  • 9
  • Probably a dup of https://stackoverflow.com/questions/563406/how-to-add-days-to-date. Instead of calculating and adding a big number of ms, use the getDate() function in JS date. Now is `let d = new Date();` 14 days from now is `d.setDate(d.getDate() + 14)` – danh Aug 14 '22 at 23:17
  • You need this: https://day.js.org/docs/en/manipulate/add Now you can do this: https://jsfiddle.net/7uhayf4e/ –  Aug 15 '22 at 00:46
  • Thanks to everyone for your help. Very much appreciated. – aye decoder Aug 15 '22 at 03:34

1 Answers1

-1

Just add 60 * 60 * 24 * 14 * 1000 to today's timestamp:

const today = new Date();
const future = new Date(today.getTime() + (60 * 60 * 24 * 14) * 1000);

console.log("today:", today)
console.log("2 weeks from now:", future);
vanowm
  • 9,466
  • 2
  • 21
  • 37