JSFiddle - React, Tailwind, and code Playground
by jsumners
HTML
<p>Last Sunday's date was ~ <span id="sunDate"></span> ~.</p>
JavaScript
$(function() {
var myDate = {
addDays: function(days) {
this.date.setDate(this.date.getDate() + days);
return this.date;
},
date: {},
getDayOfYear: function() {
var jan1 = new Date(this.date.getFullYear(), 0, 1);
return Math.ceil((this.date - jan1) / 86400000);
},
setToSunday: function() {
this.addDays(this.date.getDay() * -1);
return this;
},
init: function() {
this.date = new Date();
delete this.init;
return this;
}
}.init(),
sunday = myDate.setToSunday();
$("#sunDate").html(sunday.date.toLocaleDateString());
});