Skip to main content

Posts

Showing posts from November, 2018

JavaScript (and Python): Get All Sundays From Current Month

JavaScript Before we start, I was impressed by this brilliancy on StackOverflow . Thus I apply the similar concept in JavaScript. Prior to that enlightenment , I looped through the whole month then picked each Sunday which was fine. But using that concept, we can loop weekly or every 7 days, which is way nicer. We need to pay attention on how to get the correct first Sunday of the month. In JavaScript, Sunday is at index 0 in its weekdays construction, through .getDay() (of a Date object) method: 0 is Sunday 1 is Monday 2 is Tuesday 3 is Wednesday 4 is Thursday 5 is Friday 6 is Saturday function get_sundays(a) { // Initiate empty array to store all Sunday indexes const sundays = []; // Default current date definition as input let current = new Date(); // There's valid input date if (a?.constructor === Date) { current = a; // Input date } // First date of current month const date = new Date( ...