0

I'm trying to display the difference between the 1st of October 2013 and today.

According to Microsoft's calculator date difference tool it is 274 days.

But with my current javascript code, I get a result of 244 days.

My current code:

  <script type="text/javascript">

    var pregnant = new Date(2013, 10, 1) 
    today=new Date()
    var one_day=1000*60*60*24

    var difference =  Math.ceil((today.getTime()-pregnant.getTime())/(one_day));

    document.write("It has been " + difference + " days since Jackie has been pregnant!")

</script>

Also I need some help with placing the content in a specific div rather than just throwing it in the html somewhere.

vlovystack
  • 703
  • 3
  • 11
  • 20

1 Answers1

1

Months are zero based, so october would be the ninth month in javascript, not the tenth.

var pregnant = new Date(2013, 9, 1);

To put the result in an element, you do something like

document.querySelector('element_id').innerHTML = "It has been " + difference + " ....";
adeneo
  • 312,895
  • 29
  • 395
  • 388