I have a what seems like simple date issue and I just can’t wrap my head around trying to get it efficiently… I basically need to get the previous months date for a specific day.
For example: If today is the 3rd Thursday of the month, I want to get the 3rd Thursday’s date of last month. Its important that its based of the number of the day…ie: First Monday, 4th Friday, 2nd Wednesday, etc.
What’s the best way to get this done?
BTW…If there is not an equivalent previous months day that is fine. I can handle that. Also, currently I am counting the number or days (“Mondays”, “Tuesdays”, etc) manually to figure this out. I was just hoping there is a more elegant way to do it.
Here’s what I would do:
Usage example:
N.B. :
getting the ordinal position of the day of week inside a month is really easy: