Javascript Data Type How to - Subtract days from a date








Question

We would like to know how to subtract days from a date.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>
<script type='text/javascript'>
<!--   w ww . jav a 2 s  .c o  m-->
var videodate = new Date(2014,7,5);
var previousdate  = new Date(videodate.getTime() - (24 * 60 * 60 * 1000));
document.writeln(videodate);
document.writeln('<br/>');
document.writeln(previousdate);

var videodate = new Date(2014,7,5);
var previousdate = moment(videodate).subtract('days', 1);
document.writeln(videodate);
document.writeln('<br/>');
document.writeln(previousdate);

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: