0

How can i add 5 days to the current date, and then convert it to a string representing the local date and time?

const newDate = new Date();

const test = newDate.setDate(newDate.getDate() + 5).toLocaleString();

Just returns the number of milliseconds.. (same if i use toString().

Gambit2007
  • 2,462
  • 5
  • 33
  • 62
  • Does this answer your question? [Add days to JavaScript Date](https://stackoverflow.com/questions/563406/add-days-to-javascript-date) – maraaaaaaaa Jan 17 '22 at 16:54

3 Answers3

1

Without using any libraries, Vanilla JS solution:

const now = new Date()
const inFiveDays = new Date(new Date(now).setDate(now.getDate() + 5))
console.log('now', now.toLocaleString())
console.log('inFiveDays', inFiveDays.toLocaleString())

This even works when your date overflows the current month.

gru
  • 1,305
  • 4
  • 12
  • 24
0

The easiest way is by using a date library like Moment.js or date-fns. I gave an example below using date-fns and addDays

const today = new Date();
const fiveDaysLater = addDays(newDate, 5);
Nathan Wiles
  • 704
  • 9
  • 28
0

Just use new Date() in front of it.

const newDate = new Date();
const add  =5
const test = newDate.setDate(newDate.getDate() + add)
console.log(new Date(test).toLocaleString());
James
  • 2,608
  • 2
  • 2
  • 23