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
Remember, month in JavaScript is always minus 1 the actual month (zero-based array index).
October is the 10th Gregorian, then in JavaScript it's 9.
Anyhow, for the function above, the input argument can be left empty.
Output
The output from the function will be array of integers.
For instance, November 2018, will yield [4, 11, 18, 25]
If you look the calendar, November 4, 2018 is Sunday, and so the 11th, 18th, and 25th
Python
In Python, Sunday is at index 6. Because Python is a name of a gigantic snake before it became a TV show then a programming language, so it doesn't have correlation with earlier sentence. What sentence?
Using .weekday()
(of a datetime object) method:
- 0 is Monday
- 1 is Tuesday
- 2 is Wednesday
- 3 is Thursday
- 4 is Friday
- 5 is Saturday
- 6 is Sunday
Fortunately, the snippet above can be used in Python 3 environment.
In Python, the month integer is the same as the actual Gregorian. December is 12, September is 9, March is 3, January is 1, and all. Any integer from 1 to 12.
Only weekday is using 0. Any integer from 0 to 6, where 0 is Monday, 6 is Sunday.
Output
Exactly the same as the JavaScript snippet, a list of integers.
Comments
Post a Comment