Monkey Raptor

Thursday, November 29, 2018

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

JavaScript

Before I start, I was impressed by this brilliancy on StackOverflow. Thus, I also apply the concept to JavaScript. First, 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 nicer, I suppose. The thing to pay attention more carefully is to get the correct first Sunday of the month.

Alright, let's go. In JavaScript, Sunday is at index 0 in its weekdays construction, um. Or other wordings. The .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

(Same as C language since JavaScript is C based)

(Not the coding below, but the array position above)

Input

It receives Date object only. To construct a new date:

Remember, month in JavaScript is always minus 1 the actual month.

October is the 10th Gregorian dilly, then in JavaScript it's 9. Hahaha. Uhm. Kinda confusing at first, but not if you're used to.

It's related to index of an array. If we have [January, February, March, ..., December], surely the position of "January" in the array will be 0. Month in JavaScript is any integer from 0 to 11, where 0 is January, and 11 is December.

And also [Sunday, Monday, ..., Saturday], therefore "Sunday" is at index 0. Thus, weekdays index in JavaScript ranges from 0 to 6 (integer), where 0 is Sunday and 6 is Saturday.

In English, "weekday" is any day of the week other than Sunday (and Saturday). "Working day" in another words *twinkle twinkle*

Anyhow, you can leave the input argument to be empty or undefined to get current month's Sundays.

Output

It 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

Input

Input type must be date object. Or, you can leave it empty to get current month.

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.


More tinkering

It's up to you, whatever modification of input / process / output that suits your progrem. In this post, I shared my new learning of "looping weekly". I suppose the concept can be broadened and applied to other matters, not just calendar related.


How about public holidays?

Well, we need a regularly updated database of course and read that list. Every country has its own different holidays. There're services we can use, free or paid, for instance Google Calendar or TimeAndDate, or others. Some countries (government sites) provide free access to get the list of holidays of current year, perhaps. I mean like an API, not just displaying it on the site's page. But not every country.

Or, we could create our own DB "manually".

JavaScript (and Python): Get All Sundays From Current Month
https://monkeyraptor.johanpaul.net/2018/11/javascript-and-python-get-all-sundays.html

No comments

Post a Comment

Tell me what you think...