1

var d = new Date(),
datestring = '';

datestring = d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate()+

d.setDate(d.getDate() + 50);
document.getElementById("new").innerHTML = d;
<p>Your estimated date for completion will be </p>


<p id="new"></p>

Newbie here. Trying to display the date without time and add 500 days. Thanks in advance for your help

kevinSpaceyIsKeyserSöze
  • 3,693
  • 2
  • 16
  • 25
  • 1
    You need to compute datestring after calling setDate. Something like this d.setDate(d.getDate() + 50); datestring = d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate() document.getElementById("new").innerHTML = datestring; – SPS Nov 26 '19 at 09:05
  • please check this https://stackoverflow.com/questions/3818193/how-to-add-number-of-days-to-todays-date – Kankatala Krishna Nov 26 '19 at 09:08

2 Answers2

2

var today = new Date();
alert("Today = " + today.toLocaleDateString());
Date.prototype.addDays = function(days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}
var date = new Date();
date = today.addDays(5)
alert("After 5 days = " + date.toLocaleDateString());
Thameem
  • 700
  • 1
  • 13
  • 38
  • hi Thameem Thank you. How do I get rid of the alert commands and get it to display on the screen? – Chris Parry Nov 27 '19 at 10:07
  • 1. Create any text element in your html code like span, p, h1-h6 ... 2. Assign an id to that element 3. use "document.getElementbyId("YOUR_ELEMENT_ID").innerHTML = YOUR_VALUE" I think you should go with this link => https://www.w3schools.com/js/ – Thameem Nov 27 '19 at 11:40
  • That's ok No Problem. – Thameem Nov 28 '19 at 11:36
0

If you want to show only the date then it will be like following,

var d = new Date();
d.setDate(d.getDate() + 50);
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var date = d.getDate()+' '+months[d.getMonth()]+', '+d.getFullYear(); 
document.getElementById("new").innerHTML = date;
Shattique
  • 118
  • 6
  • Thank you. I can't get that to work, I added to my page , but it won't display the date ! – Chris Parry Nov 27 '19 at 10:08
  • do you have

    in your page? also I have added the initial code that was present in your previous code. You need to add var d = new Date(); first.
    – Shattique Nov 27 '19 at 10:58