admin管理员组文章数量:1355697
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());
本文标签: javascriptAdd days to Date object and then converting to local stringStack Overflow
版权声明:本文标题:javascript - Add days to Date object and then converting to local string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744031059a2578914.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论