How to find number of thursdays in a month?

4
My firstcreated date is 01/06/2021 and end of month is 30/06/2021, I want find number of thursdays in a month.
asked
3 answers
4

Hi Janani,

You can use parseInteger(formatDateTime($date, 'u')) to get day of the week (as a number) from a Date and time attribute so it can be used to compare to a value.

loop between two dates and check the day is equals 5.

I hope it will be helpful for you.

 

answered
3

Given month x, how many days y does it contain? It is always a challenge to create a program that solves this kind of questions. Very rewarding once you have succeeded. But it is always more complex than expected at first, the solution is often hard to read and understand and edge-case hide in corners where you don’t expect them and it should therefor be unittested thoroughly.

To the solution, untested, just jotting down the steps:

- yourday is an integer from 1 to 7

- get the first day (type date) of the month. Include a variable ‘year’ to take leap-years into account; parseDateTime($Year, $Month, 1)

- get the last day (type date) of the month: parseDateTime($Year, $Month+1, 1) unless month =12 then: parseDateTime($Year+1, 1, 1). Wrap that answer in addDays($FirstOfNextMonth, -1)

- get the number of days in the month by daysBetween(firstday, lastday)

- if this number is:

  • 28: your answer is 4
  • 29: if weekday(firstday) is yourday (thursday) then 5 else 4
  • 30: if (mod(weekday(firstday) – yourday,7)) = 0 then 5 else 4
  • 31: if (mod(weekday(firstday) – yourday,7)) = 0 or 1 then 5 else 4

 

Test it to the bone and certainly for every edgecase:

- feb 28th , feb 29th , mar1st for 2022, 2023, 2024, 2028, 2032

- dec 31st , jan 1st for 2022, 2023, 2024, 2028, 2032

Good luck and have fun.

 

answered
2

You  can get count no of particular day  in a month by using

“ XPath Weekday-from-DateTime “

 Overview

The weekday-from-dateTime() function extracts the day of the week (as a number) from a Date and time attribute so it can be used to compare to a value. For running locally and deployment using a PostgreSQL database, such as that used in the Mendix Cloud, the values range from 1 to 7 (1 = Sunday, 7 = Saturday).

 Example

This query returns all the logs where the day of the week in DateAttribute is 6 (Friday, for locally run apps or apps using a PostgreSQL database):

//Logging.Log[weekday-from-dateTime(DateAttribute) = 6]

answered