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()
.
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()
.
- Does this answer your question? Add days to JavaScript Date – maraaaaaaaa Commented Jan 17, 2022 at 16:54
3 Answers
Reset to default 5Without 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.
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 newDate = new Date();
const fiveDaysLater = addDays(newDate, 5);
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());