Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Last occurrence of a day of the month

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 526

blank.gif
PostPosted: Sun Oct 04, 2020 6:22 pm    Post subject: Last occurrence of a day of the month Reply with quote

I've been using the following to determine the nth occurrence of a day of the month and it works well:

Code:
Function DayNameCountCurrentMonth() As Byte

  Local Temp_day As Integer
  Local Day_Name_Counter as Byte

  Day_Name_Counter = 1
  Temp_day = _day                                             'here we get a number from 1 to 31 (Current system date).
  Temp_day = Temp_day - 7                                     'previous week

  While Temp_day > 0
    Incr Day_Name_Counter
    Temp_day = Temp_day - 7                                  'previous week
  Wend

  DayNameCountCurrentMonth = Day_Name_Counter

End Function


This makes it easy to determine, for example, the 3rd Wednesday of the month. Except I need to be able to determine the last occurrence of a day of the month (let's say the last Friday). As some months have 4 weeks and others have 5, well..that's my problem.

(BASCOM-AVR version : 2.0.8.3 )
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2335

blank.gif
PostPosted: Sun Oct 04, 2020 7:38 pm    Post subject: Re: Last occurrence of a day of the month Reply with quote

KenHorse wrote:
I've been using the following to determine the nth occurrence of a day of ethe month and it works well:

The shown function does nothing more than retrieving the n-th week in a month for a certain numbered day, it does not retrieve the name of said day.
Quote:
This makes it easy to determine, for example, the 3rd Wednesday of the month

'This' for sure not. Possible however in combination with other, not shown functions.
Quote:
Except I need to be able to determine the last occurrence of a day of the month (let's say the last Friday). As some months have 4 weeks and others have 5, well..that's my problem.

Calculate the date of the 5th occurence, then check if the result's month is the same as the month in request.
If not, the 4th occurence is valid.
I do remember this issue some while ago here in the forum.
Afair there were two competing solutions, my solution was to use Bascom's convert functions.
You may look that up, it provides the desired solution.
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum