Javascript Data Type How to - Add a number of days to a date








Question

We would like to know how to add a number of days to a date.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.8.3.js'></script>
<style type='text/css'>
span {<!--from w  w  w .j  a  v  a2 s  . co  m-->
  display: block;
  margin-bottom: 5px;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
    var today = new Date();
    var yesterday = new Date();
    yesterday.setDate(today.getDate() - 1);
    var tomorrow = new Date();
    tomorrow.setDate(today.getDate() + 1);
    $("#yesterday").text("Yesterday: " + yesterday.toDateString());
    $("#today").text("Today: " + today.toDateString());
    $("#tomorrow").text("Tomorrow: " + tomorrow.toDateString());
});
</script>
</head>
<body>
  <span id="yesterday"></span>
  <span id="today"></span>
  <span id="tomorrow"></span>
</body>
</html>

The code above is rendered as follows: