Javascript Data Type How to - Get how many 'Weeks Ago'








Question

We would like to know how to get how many 'Weeks Ago'.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!--   ww  w  .  j  a v a 2  s  .co  m-->
function dateDiffInWeeks(a,b) {
  var _MS_PER_WEEK = 1000 * 60 * 60 * 24 * 7;
  // Discard the time and zone
  var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
  var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
  return Math.floor((utc2 - utc1) / _MS_PER_WEEK);
}
var today = new Date();
var anotherDay = new Date("Fri Nov 20 2009");
document.writeln(dateDiffInWeeks(today,anotherDay));
}
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: