Javascript Data Type How to - Check weeks, days and years difference in moment.js








Question

We would like to know how to check weeks, days and years difference in moment.js.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.7.0/moment.js"></script>
<!--from   w  w  w.j  ava2s. co m-->
<script type='text/javascript'>
var EndDate = moment([2008, 0, 29]);//End Date
var StartDate = moment([2007, 0, 28]);//Start Date
document.writeln("Weeks : " + EndDate.diff(StartDate, 'weeks'));
document.writeln('<br/>');
document.writeln("Days : " + EndDate.diff(StartDate, 'days'));
document.writeln('<br/>');
document.writeln("Years : " + EndDate.diff(StartDate, 'years'));
document.writeln('<br/>');
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: