Forum - MCS Electronics

 Calculating date from # of occurance of Sunday in a month? Goto page 1, 2  Next
Author Message
KenHorse

Joined: 16 Jul 2004
Posts: 452

Posted: Wed Jul 10, 2019 6:00 pm    Post subject: Calculating date from # of occurance of Sunday in a month?

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 )
Evert :-)

Joined: 18 Feb 2005
Posts: 2088

 Posted: Wed Jul 10, 2019 7:40 pm    Post subject: 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
KenHorse

Joined: 16 Jul 2004
Posts: 452

 Posted: Wed Jul 10, 2019 9:35 pm    Post subject: Indeed I have. In fact, I've been playing with it trying to accomplish what I need.
MWS

Joined: 22 Aug 2009
Posts: 1686

Posted: Thu Jul 11, 2019 8:33 am    Post subject:

Ken,

from what I did experience with you, especially your most unclear problem descriptions, I was thinking about not answering anymore any of your requests.
In this case at least your code tells what you want to achieve.
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?
KenHorse

Joined: 16 Jul 2004
Posts: 452

 Posted: Thu Jul 11, 2019 4:32 pm    Post subject: Thank you MWS
KenHorse

Joined: 16 Jul 2004
Posts: 452

 Posted: Thu Jul 11, 2019 8:25 pm    Post subject: 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?
MWS

Joined: 22 Aug 2009
Posts: 1686

 Posted: Fri Jul 12, 2019 6:46 am    Post subject: 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.
Evert :-)

Joined: 18 Feb 2005
Posts: 2088

 Posted: Fri Jul 12, 2019 9:29 am    Post subject: Excel formula that you can convert: https://exceljet.net/formula/get-nth-day-of-week-in-month_________________www.evertdekker.com Bascom code vault
KenHorse

Joined: 16 Jul 2004
Posts: 452

Posted: Wed Jul 17, 2019 9:10 pm    Post subject:

 Evert 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....
MWS

Joined: 22 Aug 2009
Posts: 1686

Posted: Wed Jul 17, 2019 10:51 pm    Post subject:

 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.
KenHorse

Joined: 16 Jul 2004
Posts: 452

Posted: Wed Jul 17, 2019 10:58 pm    Post subject:

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.......
MWS

Joined: 22 Aug 2009
Posts: 1686

Posted: Thu Jul 18, 2019 12:38 am    Post subject:

 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.
KenHorse

Joined: 16 Jul 2004
Posts: 452

 Posted: Thu Jul 18, 2019 5:58 pm    Post subject: 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
MWS

Joined: 22 Aug 2009
Posts: 1686

Posted: Thu Jul 18, 2019 9:05 pm    Post subject:

 KenHorse wrote: am wasting too much time on it.

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.
six1

Joined: 27 Feb 2009
Posts: 526

Posted: Fri Jul 19, 2019 7:44 am    Post subject:

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.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT + 1 HourGoto page 1, 2  Next Page 1 of 2

 Jump to: Select a forum BASCOM AVR/8051----------------BASCOM-AVRBASCOM-8051BASCOM-ARDUINOShare your working BASCOM-8051 code hereShare your working BASCOM-AVR code hereBASCOM BETA-SLA BASCOM Related----------------EASY TCP/IPAVR-DOSAR7212KokkeKat FAT-free SD card libBASCOM Project Blog Other Stuff----------------VariousPCB'sRoboticsNew WebSiteAnnouncementsAVR Archive----------------BASCOM-AVR ArchiveBASCOM-8051 ArchiveBASCOM-AVR Unsupported versionsEasy TCP/IP ArchiveBASCOM-EDB
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