JBASE- function to calculate the 2nd, 3rd and 4th monday of every month

1,232 views
Skip to first unread message

MRM

unread,
Jan 7, 2013, 9:00:17 AM1/7/13
to jb...@googlegroups.com
Hi,

Is there a function to calculate e.g 3rd monday of every month. I know of OCONV but that returns the day.

Anyone out there kindly help.


Regards,

MRM

Daniel Klein

unread,
Jan 7, 2013, 10:46:00 AM1/7/13
to jb...@googlegroups.com
No, but it would be pretty simple to write using the 'DJ' (Julian date) conversion code. Simply calculate he 1st Monday of the month, e.g.

0001     today = DATE()
0002     mth = OCONV(today, 'DM')
0003     yr = OCONV(today, 'DY')
0004     first_of_month = ICONV(mth:'-1-':yr, 'D')
0005     day_of_week = OCONV(first_of_month, 'DW')
0006     first_monday = first_of_month + (8-day_of_week)
0007     CRT OCONV(first_monday, 'D'):' is the 1st Monday of this month.'

then add 7, 14 and 21 days (to 'first_monday') to get the 2nd, 3rd and 4th Mondays respectively.

That being said, rather than do the calculation, it might be better (simpler) to use an external table.



--
--
IMPORTANT: T24/Globus posts are no longer accepted on this forum.

To post, send email to jB...@googlegroups.com
To unsubscribe, send email to jBASE-un...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/jBASE?hl=en




Daniel Klein

unread,
Jan 7, 2013, 10:48:35 AM1/7/13
to jb...@googlegroups.com
Forget about that 'julian date' bit.

Kevin Powick

unread,
Jan 7, 2013, 12:12:30 PM1/7/13
to jb...@googlegroups.com


On Monday, 7 January 2013 10:46:00 UTC-5, Daniel Klein wrote:
That being said, rather than do the calculation, it might be better (simpler) to use an external table.


Why an external table?  The simple calculation below will give one the 3rd Monday for any given month/year.  We know that the maximum date of any 3rd Monday is the 21st of the month, so we work with that as follows.

PROMPT ''
CRT 'YEAR (YYYY) ':
INPUT YEAR
IF YEAR = '' THEN STOP

CRT 'MONTH (MMM) ':
INPUT MONTH
IF MONTH = '' THEN STOP

TARGET = ICONV('21':MONTH:YEAR,'D4')
DAY.OF.WEEK = OCONV(TARGET,'DW')
THIRD.MONDAY = TARGET - (DAY.OF.WEEK - 1)

CRT OCONV(THIRD.MONDAY, 'D2')
 
A shorter version for the current date.

TODAY = DATE()
YEAR  = OCONV(TODAY,'DY')
MONTH = OCONV(TODAY,'DMA')

TARGET       = ICONV('21':MONTH:YEAR,'D4')
DAY.OF.WEEK  = OCONV(TARGET,'DW')
THIRD.MONDAY = TARGET - (DAY.OF.WEEK - 1)

CRT OCONV(THIRD.MONDAY, 'D2')

BTW, the above is for D3, but I think it should work for jBASE either unchanged or only needing a minor tweak

Kevin Powick

Kevin Powick

unread,
Jan 7, 2013, 12:28:57 PM1/7/13
to jb...@googlegroups.com
Ok, I see the subject line of the OP is actually asking for more than the content, of the post.  I just focused on the 3rd Monday.  Oh, well.  There's enough information now available that the poster should be able to write a complete solution.

Kevin Powick

Kevin Powick

unread,
Jan 7, 2013, 3:59:20 PM1/7/13
to jb...@googlegroups.com


On Monday, 7 January 2013 10:46:00 UTC-5, Daniel Klein wrote:

0001     today = DATE()
0002     mth = OCONV(today, 'DM')
0003     yr = OCONV(today, 'DY')
0004     first_of_month = ICONV(mth:'-1-':yr, 'D')
0005     day_of_week = OCONV(first_of_month, 'DW')
0006     first_monday = first_of_month + (8-day_of_week)
0007     CRT OCONV(first_monday, 'D'):' is the 1st Monday of this month.'


That code actually doesn't work in all cases.  For example, try April 2013

The following should give the 1st through 4th Mondays for any given month and year:

TODAY = DATE()
YEAR  = OCONV(TODAY,'DY')
MONTH = OCONV(TODAY,'DMA')

FIRST.OF.MONTH = ICONV('1':MONTH:YEAR,'D4')
DAY.OF.WEEK    = OCONV(FIRST.OF.MONTH,'DW')

IF DAY.OF.WEEK = 1 THEN
   FIRST.MONDAY = FIRST.OF.MONTH
END ELSE
   FIRST.MONDAY = FIRST.OF.MONTH + (8 - DAY.OF.WEEK)
END

FOURTH.MONDAY = FIRST.MONDAY + 21

FOR MONDAY = FIRST.MONDAY TO FOURTH.MONDAY STEP 7
   CRT 'MONDAY = ':OCONV(MONDAY,'D2')
NEXT MONDAY

--
Kevin Powick

Daniel Klein

unread,
Jan 7, 2013, 5:48:01 PM1/7/13
to jb...@googlegroups.com
It's an easy fix...

0001     today = 16528   ;*April Fools Day
0002     mth = OCONV(today, 'DM')
0003     yr = OCONV(today, 'DY')
0004     first_of_month = ICONV(mth:'-1-':yr, 'D')
0005     day_of_week = OCONV(first_of_month, 'DW')
0006     IF day_of_week = 1 THEN
0007         first_monday = today
0008     END ELSE
0009         first_monday = first_of_month + (8-day_of_week)
0010     END
0011     CRT OCONV(first_monday, 'D'):' is the 1st Monday of this month.'


--

Kevin Powick

unread,
Jan 7, 2013, 6:48:23 PM1/7/13
to jb...@googlegroups.com


On Monday, 7 January 2013 17:48:01 UTC-5, Daniel Klein wrote:
It's an easy fix...

Yes, which I provided ;)

--
Kevin Powick

Daniel Klein

unread,
Jan 8, 2013, 5:50:59 AM1/8/13
to jb...@googlegroups.com
Of which I gave you a head start.


--

Kevin Powick

unread,
Jan 8, 2013, 9:25:59 AM1/8/13
to jb...@googlegroups.com


On Tuesday, 8 January 2013 05:50:59 UTC-5, Daniel Klein wrote:
 
Of which I gave you a head start.

 
Is buggy code really ever a "head start"? ;)

I could have lost millions.

--
Kevin Powick 

Daniel Klein

unread,
Jan 8, 2013, 10:10:13 AM1/8/13
to jb...@googlegroups.com
For you it is, since you insist on dragging this out.

Kevin Powick

unread,
Jan 8, 2013, 3:24:46 PM1/8/13
to jb...@googlegroups.com

On Tuesday, 8 January 2013 10:10:13 UTC-5, Daniel Klein wrote:
For you it is, since you insist on dragging this out.


Says Mr. Kettle.

--
Kevin Powick 

MRM

unread,
Jan 9, 2013, 4:09:52 AM1/9/13
to jb...@googlegroups.com
Thanks a lot for the head -ups, really appreciate your input! Will the same formula apply for example if i was looking for Tuesday, Wednesday, Thursday or Friday?

Thanks

Kevin Powick

unread,
Jan 9, 2013, 11:00:13 AM1/9/13
to jb...@googlegroups.com


On Wednesday, 9 January 2013 04:09:52 UTC-5, MRM wrote:
Thanks a lot for the head -ups, really appreciate your input! Will the same formula apply for example if i was looking for Tuesday, Wednesday, Thursday or Friday?

No, not without some changes.

--
Kevin Powick

Daniel Klein

unread,
Jan 9, 2013, 11:43:34 AM1/9/13
to jb...@googlegroups.com
Here you go:

0001     CRT 'Enter a date (e.g. 1/9/2013)':
0002     INPUT date
0003     date = ICONV(date, 'D')
0004     mth = OCONV(date, 'DM')
0005     yr = OCONV(date, 'DY')
0006     first_of_month = ICONV(mth:'-1-':yr, 'D')
0007     day_of_week = OCONV(first_of_month, 'DW')
0008     FOR day = 1 TO 7
0009         BEGIN CASE
0010         CASE day_of_week = day
0011             target_day = first_of_month
0012         CASE day_of_week > day
0013             target_day = first_of_month + (day + 7 - day_of_week)
0014         CASE day_of_week < day
0015             target_day = first_of_month + day - day_of_week
0016         END CASE
0017         CRT OCONV(target_day, 'D'):' is the 1st ':OCONV(target_day, 'DWA')'MCT':' of this month.'
0018         CRT OCONV(target_day+7, 'D'):' is the 2st ':OCONV(target_day+7, 'DWA')'MCT':' of this month.'
0019         CRT OCONV(target_day+14, 'D'):' is the 3st ':OCONV(target_day+14, 'DWA')'MCT':' of this month.'
0020         CRT OCONV(target_day+21, 'D'):' is the 4st ':OCONV(target_day+21, 'DWA')'MCT':' of this month.'
0021         CRT
0022     NEXT day

Dan

Kevin Powick

unread,
Jan 9, 2013, 12:51:11 PM1/9/13
to jb...@googlegroups.com

On Wednesday, 9 January 2013 11:43:34 UTC-5, Daniel Klein wrote:
Here you go:

0001     CRT 'Enter a date (e.g. 1/9/2013)':

Doesn't work for me..
 
0006     first_of_month = ICONV(mth:'-1-':yr, 'D')


 Oh yeah.  I'm not in the US. ;)

--
Kevin Powick 

Daniel Klein

unread,
Jan 9, 2013, 1:40:24 PM1/9/13
to jb...@googlegroups.com
It works for me and I'm from Earth.



--
Kevin Powick 

--
Reply all
Reply to author
Forward
0 new messages