Javascript Data Type How to - Get the most recently occurring Sunday








Question

We would like to know how to get the most recently occurring Sunday.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from w w  w.  j  a v a 2s.  c  o m-->
var now = new Date();
var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var lastSunday = new Date(today.setDate(today.getDate()-today.getDay()));
$('#now').html(now .toString());
$('#lastSunday').html(lastSunday.toString());
});
</script>
</head>
<body>
  <div id="now"></div>
  <div id="today"></div>
  <div id="lastSunday"></div>
</body>
</html>

The code above is rendered as follows: