Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Calculating date from # of occurance of Sunday in a month?
Goto page 1, 2, 3  Next
 
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: 523

blank.gif
PostPosted: Wed Jul 10, 2019 6:00 pm    Post subject: Calculating date from # of occurance of Sunday in a month? Reply with quote

So I've been using the following to determine the nth occurrence of a day within a month

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


So far, so good

But how to determine the date of a specific weekly occurrence of a day based on the actual date? In other words, if I have a date of 07.24.19, how can I calculate that is the 4th Wednesday of July?

(BASCOM-AVR version : 2.0.8.1 )
Back to top
View user's profile
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2156

netherlands.gif
PostPosted: Wed Jul 10, 2019 7:40 pm    Post subject: Reply with quote

Are you aware of the https://avrhelp.mcselec.com/datetime.htm date time function's in Bascom?

I think you can come with DayOfWeek and SysDay where you want.
At the moment I don't have a running Bascom to test somethings

_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 523

blank.gif
PostPosted: Wed Jul 10, 2019 9:35 pm    Post subject: Reply with quote

Indeed I have. In fact, I've been playing with it trying to accomplish what I need.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Thu Jul 11, 2019 8:33 am    Post subject: Reply with quote

Ken,

Btw., your code-fuss can be simply replaced by:
Code:
Temp_Day = _DAY + 6
Day_Name_Counter = Temp_Day / 7

Quote:
if I have a date of 07.24.19, how can I calculate that is the 4th Wednesday of July?

Try only one time to read the help.
It's great, it's fantastic, it contains samples (even colored pictures) and as Evert pointed out: it is also on the Internet.
Quote:
Target = DayOfWeek(bDayMonthYear)
...
The Return-Value is in the range of 0 to 6, Monday starts with 0.

Code:
Dim dateArr(3) As Byte

dateArr(1) = 24
dateArr(2) = 7
dateArr(3) = 19

  dw = DayOfWeek(dateArr)

The result of dw = 2 tells you that it is a Wednesday and 24 used as value for first code's _DAY, it is the 4th of same.

Be aware, that a 'Config Clock' needs to be somewhere in your code, otherwise the compiler throws errors.

You've noticed that your central question:
Quote:
how can I calculate that is the 4th Wednesday of July?
differs from the topic named:
Quote:
Calculating date from # of occurance of Sunday in a month?
Back to top
View user's profile
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 523

blank.gif
PostPosted: Thu Jul 11, 2019 4:32 pm    Post subject: Reply with quote

Thank you MWS
Back to top
View user's profile
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 523

blank.gif
PostPosted: Thu Jul 11, 2019 8:25 pm    Post subject: Reply with quote

Ok. so far so good

Now, suppose I want to know the date of the 3rd occurrence of a Tuesday of a given month?

For example, all I know is the month (1-12) and 3rd occurrence of Wednesday in that month. How do I determine the actual date of that day, for the current year?

Is this where DayofYear might come in handy?
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Fri Jul 12, 2019 6:46 am    Post subject: Reply with quote

If you know month and year, you can calculate the day of week for the first day of said month.
In case it's a Monday, the first Wednesday will be the 3rd, thus the third Wednesday will be the 17th.
Plausibility checks for the result need to be done, here again the date/time functions come handy.
Back to top
View user's profile
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2156

netherlands.gif
PostPosted: Fri Jul 12, 2019 9:29 am    Post subject: Reply with quote

Excel formula that you can convert: https://exceljet.net/formula/get-nth-day-of-week-in-month
_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 523

blank.gif
PostPosted: Wed Jul 17, 2019 9:10 pm    Post subject: Reply with quote

Evert Smile wrote:
Excel formula that you can convert: https://exceljet.net/formula/get-nth-day-of-week-in-month


That appears to backwards from what I need I believe

The only input I have to work with is knowing the occurrence of a day within a particular month.

For example, let's say I want to find the date (for the current year) of the 4th occurrence of Tuesday in July. Looking at a calendar, I can see that to be 07/23/19.

Fine, how to calculate the date in Bascom?

Sorry, I have a nasty habit of not making myself clear. Hopefully I succeeded this time....
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Wed Jul 17, 2019 10:51 pm    Post subject: Reply with quote

KenHorse wrote:
I have a nasty habit of not making myself clear.

Thou shalt not extend the habit by not reading nor understanding answers given to you.
Back to top
View user's profile
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 523

blank.gif
PostPosted: Wed Jul 17, 2019 10:58 pm    Post subject: Reply with quote

MWS wrote:
KenHorse wrote:
I have a nasty habit of not making myself clear.

Thou shalt not extend the habit by not reading nor understanding answers given to you.


I was willing to pay a reasonable price for the code but I guess not.......
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Thu Jul 18, 2019 12:38 am    Post subject: Reply with quote

KenHorse wrote:
I was willing to pay a reasonable price for the code but I guess not.......

I'm sorry, I'm not here for getting paid, I post for fun.
You can try to convert the Excel thing, which in contrary to your belief actually does what you want, i.e. it does what you describe.
Your input is month, year and DOW, the desired output is an exact date, the Excel sheet shows it in row 'Result'.
The Excel formula works similar to my suggestion in determining the DOW for the first day of month and then uses this result to calculate the exact date, which is easy.
For example, you know the year, 2019, you know the month, say August, 08, you know the first of month, wich is of course 01.
This gives 08.01.2019, Bascom's DayOfWeek() will return 4, as it's a Thursday.
If you look for the n't Friday of same month, while you know the first day is a Thursday, naturally and also easy to calculate, the first Friday must be the second of August.
Assumed you look for the 4th Friday of same month, you add 3 * 7 = 21 days to the 2nd, which returns the 23rd.
Finally you need do do range checking for not using void combinations.
Back to top
View user's profile
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 523

blank.gif
PostPosted: Thu Jul 18, 2019 5:58 pm    Post subject: Reply with quote

Then perhaps someone else will be interested in code for payment as I need to get this handled and am wasting too much time on it.

If anyone is interested, contact me at kenhorse3@gmail.com
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Thu Jul 18, 2019 9:05 pm    Post subject: Reply with quote

KenHorse wrote:
am wasting too much time on it.
I do not want to embarrass you, but I was wondering about this simple problem and especially your resistance to listen to suggestions made me curios.

Had this code ready in really short time, which was due to Bascom's integrated functions.
Code works as I suggested, effective core code including plausibility check is 13 lines, overall code is 57 lines.

Bascom's functions DayOfWeek(), SysDay(), Date() were used and highly simplified the task.
I won't post the code, as a) I told you not to take money while in the same I'm not supposed to do your work and b) it would be unfair to anyone who offers you the paid service as you've requested.
Back to top
View user's profile
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 553

germany.gif
PostPosted: Fri Jul 19, 2019 7:44 am    Post subject: Reply with quote

Hi Ken,
i'm not sure, if i'm understanding you the right way...

maybe this simplified code give you the right direction?

Code:

Dim dw as Byte
Dim dn as Byte

Dim dateArr(3) As Byte

dateArr(1) = 24
dateArr(2) = 7
dateArr(3) = 19

dn = _day / 7
incr dn

dw = DayOfWeek(dateArr)

print "Day " + str(_day) + " is the " + str(dn) + " of same and it is Day of week No.: " + str(dw)
 

_________________
For technical reasons, the signature is on the back of this message.
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
Goto page 1, 2, 3  Next
Page 1 of 3

 
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