-1

I want to add 30 days to today date and here how i tried Thanks in advance!

var date = new Date();
    document.getElementById("demo").value = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();

  • `new Date(new Date().getTime() + (1000 * 60 * 60 * 24 * 30))` – Shubham Jun 28 '19 at 06:27
  • You did right, but months numbering is from 0 to 11. So, to get current month you need to add 1. To get next month you should add 2. – Alex Stulov Jun 28 '19 at 06:30
  • 3
    Possible duplicate of [Add days to JavaScript Date](https://stackoverflow.com/questions/563406/add-days-to-javascript-date) and [How to add number of days to today's date?](https://stackoverflow.com/questions/3818193) – adiga Jun 28 '19 at 06:36
  • Stack Overflow has been around for more than a decade and 1.8 million javascript questions have been asked. Do you really think nobody has come with a question about adding x days to a date? Please read [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/a/261593/3082296) – adiga Jun 28 '19 at 06:36

5 Answers5

1

var date = new Date();
date.setDate(date.getDate() + 30); // add 30 days 
console.log(date);
Jaydeep
  • 1,686
  • 1
  • 16
  • 29
  • Your code might solve the problem but you should always leave a comment on why this solves the OP's problem, keep in mind that the reason we aim to help others is also to educate them. – Carsten Løvbo Andersen Jun 28 '19 at 06:32
0

Try adding this

add 30 days to todays date: var now = new Date(); now.setDate(now.getDate() + 30);

Abhishek Pandey
  • 13,302
  • 8
  • 38
  • 68
Naveen
  • 361
  • 2
  • 10
0

var date = new Date();
date.setDate(date.getDate() + 30); // Set current date + 30 days as the new date
console.log(date);

document.getElementById("demo").innerHTML = date; // to set the value to HTML
  • Thanks for reply, i want to display this date in Text field in Html can you please help me @Tanveer Singh Bhatia – mahesh siripuram Jun 28 '19 at 06:31
  • Your code might solve the problem but you should always leave a comment on why this solves the OP's problem, keep in mind that the reason we aim to help others is also to educate them. – Carsten Løvbo Andersen Jun 28 '19 at 06:32
0

With moment.js that would be:

const date = moment().add(30, 'days');
console.log(date)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
antonku
  • 7,377
  • 2
  • 15
  • 21
0

you can also use moment.js library.

it is simple yet powerful library for dates.

const moment = require('moment');
console.log(moment().add(30, 'days'));    // 2019-07-28T06:31:28.803
Usama Tahir
  • 1,707
  • 3
  • 15
  • 30