Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to calculate friday, saturday, sunday between two dates 5

Status
Not open for further replies.

fanlinux90

Programmer
Oct 31, 2022
22
0
0
CO
Between the dates 11/28/2023 and 12/02/2022 I know how many days there are. DATE(2023, 11, 28) - DATE(2022, 12, 02), how can I calculate how many Fridays, Saturdays, and Sundays there are between those two dates?
 
That's a familiar requirement.

How precise do you need it to be? An estimate would be No. Days - ((No. Days/7) * 3)
(parenthesis added for clarity).

I imagine you realise the 'easy' way is just to iterate through the days and count the number that are Fr,Sa, or Su...



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
Calculate for each month the number of Fridays, Saturdays and Sundays it has. November 2022 has four Fridays, four Saturdays and four Sundays. Is it possible to calculate those days?
 
You have the DOW() function. So for example you can determine on which weekday a month starts. From that knowledge you can compute the first Friday of the month and then also the 2nd, 3rd, 4th, and - if it doesn' go into the next month, the 5th Friday. And Saturday and Sunday.

Take a look at how DOW works in the help.

Chriss
 
By the way, even the shortest month, a usual 28 day February, has every weekday 4 times. And even in a leap year February also has all weekdays 4 times.

There is no month of length 35 days, so any weekday can only exist 4 or 5 times. So you could determine whether the 5th Friday/Saturday/Sunday are still in the same month, then know they occur 5 times, or whether they fall into the next month, then you know they occur 4 times in the current month.

Chriss
 
This is accurate, but relies on a DO WHILE loop, which most/some people would frown upon:

Code:
FUNCTION WORKDAYS
	PARAMETERS m.DATE1,m.DATE2
	PRIVATE m.NUMDAYS,m.DATE1,m.DATE2,m.TMPDATE,m.IDATE,m.COUNT
	PRIVATE m.INIBITDAYS
	m.INHIBITDAYS = "FRI;SAT;SUN;"
	IF EMPTY(m.DATE1) .OR. EMPTY(m.DATE2)
		m.NUMDAYS = 0
	ELSE
		IF m.DATE2 < m.DATE1
			m.TMPDATE = m.DATE1
			m.DATE1 = m.DATE2
			m.DATE2 = m.TMPDATE
		ELSE
			m.TMPDATE = CTOD("//")
		ENDIF
		m.IDATE = m.DATE1
		m.COUNT = 0
		DO WHILE m.IDATE < m.DATE2
			IF !UPPER(LEFT(CDOW(m.IDATE),3))+";"$m.INHIBITDAYS
				m.COUNT = m.COUNT +1
			ENDIF
			m.IDATE = m.IDATE +1
		ENDDO
		m.NUMDAYS = m.COUNT
	ENDIF
	RETURN(m.NUMDAYS)

For clarity, this does the reverse of your requirements - gives you the number of days between the two dates
that are not Friday, Saturday or Sunday. You have two options to solve your problem, either deduct that from
the number of days betwween the two dates, or modify m.INHIBITDAYS to be "MON;TUE;WED;THU;"

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
Hi,
Please have a look at the code snippet below

Code:
LPARAMETERS tdDateStart, tdDateEnd

LOCAL liWEDays, liWorkDays

liWEDays = 0
liWorkDays = 0

IF VARTYPE(tdDateStart) = "D" AND VARTYPE(tdDateEnd) = "D" AND tdDateStart < tdDateEnd

	liWEDays = 0

	FOR i = 0 TO tdDateEnd - tdDateStart
		IF CDOW(tdDateStart + i) = "Friday" OR CDOW(tdDateStart + i) = "Saturday" OR CDOW(tdDateStart + i) = "Sunday"
			liWEDays = liWEDays + 1
			
		ENDIF
	ENDFOR
	
	liWorkDays = tdDateEnd - tdDateStart - liWEDays 

	WAIT WINDOW + "Workdays: " + TRANSFORM(liWorkDays) + " - " + "WE Days: " +  TRANSFORM(liWEDays)

	
ELSE 

	= MESSAGEBOX("Wrong parameters", 16, "Checking parameters") 

ENDIF 

RETURN liWEDays

hth

MarK
 
Any feedback from you? Otherwise I assume you aren't really interested in learning how to get that solution but only want to grab off a solution.

Chriss
 
Chriss

Are you suggesting this might be a student trying to complete some coursework... ?

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
Griff,

thats not for sure, but you could expect a bit of feedback just like his response after your first post.

In short I answered his question "Is it possible to calculate those days?" with yes. With the help of DOW you know how far off the first of a month is from Fri/Sat/Sun and thus can compute firstofmonth+difference+n*7 to get thos dates.

You know how your remark to go through all days would trigger someones nerves, but when this is overall used to visually display a month you'd go through all days anyway.
One hint also is that I mainly only see an academic use of the number of weekend days including fridays. In a monthview of a calender, where rows are full weeks you can need 4 (rare, normal Febraries starting on Monday - or Sunday depending how you design your week rows) or 5 or 6 rows, and that would be more interesting to start building up the monthview. But then, this also is just an automatic side result of just starting with one week row and putting in all month days.

These questions could just come from a guy who compares languages to see which one to use. If you're new to FoxPro the first thing you should know that VFP9 is the last version, it's a discontinued product, there's more to say on that front, but it should lead to prefer other languages for a new project or for the goal to learn programming overall, even though having much VFP knowledge enables you to do new VFP projects, too. But as someone with obvious novice questions about how things work in this language, I don't know if you'll ever get warm with it and really deep into it, if you don't have to.

Sorry, Im just fabulating here. I for myself will not just post the implementation of an idea I gave, if there is no feedback of interest about it.

You know, after I gave that hint about DOW I'd be happy with mainyl two responses:
1. Okay, that helps, I can figure out the rest
2. I don't get it, can you elaborate a bit more?

But with neither repsonse...why should I continue?

Chriss
 
Fanlinux, just to amplify Chris's previous post ...

It is not the purpose of this forum to write your code for you. We can point you in the right direction, and we can explain specific language elements. But it is up to you to actually write the code.

With that in mind, here is a possible approach you can take:

1. Calculate the number of days in the specified date range (we know you know how to do that).

2. Divide that number by seven to get the number of whole weeks.

3. Take the remainder of the above division to get the number of odd days after the last whole week.

4. Determine the day number within the week (Monday = 1, etc.) of the first day in the specified date range (which is also the first day in the "odd days" period). Use DOW() for that.

5. Using the result from steps 3 and 4, determine if the target weekday lies within the odd days. If it does, add one to the result of step 2.

Give that a try, and come back if you need help with any of the details.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
There's also a simple alternative:

Fanlinux90 figured it out and therfore doesn't continue.

Chriss
 
Rude?

B-)

Others may learn something when they search for 'VFP obscure day counting criteria'

Have a great weekend one and all

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
Have you ever thought that the author of this post might otherwise be engaged? He or she posted it on the 17th some 25 hours ago, it's only the 18th as I post this.

Just saying.

Thank you

Steve Williams
VFP9, SP2, Windows 10
 
Fanlinux,

You might also like to peruse this excellent chunk of code, written by Rick Borup:


It contains a batch of date-manipulation functions of various kinds. It does not directly address your issue, but by studying the code you will get some useful insights into ways of manipulating dates within VFP. This can be helpful if you are just learning the Foxpro language.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Steve, look at the time stamps of the first few posts.

Anyway, no I'm not impatient. I'm also not saying Fanlinux90 is rude.
I just stated how I expect this to continue. And it might still.
If it does not, I haven't wasted time writing up something nobody needs.

Chriss
 
Just a side note: When a certain person benchmarked his and other peoples code, he must have used VFP6, as VFP9 can optimize some more counts (provided proper indexing) than VFP6 and you can have a result that also takes into account holidays without needing special handling for them. But that's also just lost.

Chriss
 
Chriss, I'm on your side with this one but by my own admission, I've posted a question, delayed my response in answering but as others say, if eventually there is no response or resolution, then you'll get your answer.

This is such a valuable forum.

Best wishes.

Thank you

Steve Williams
VFP9, SP2, Windows 10
 
Steve-vfp9user said:
if eventually there is no response or resolution, then you'll get your answer
I'm not sure that's exactly what you wanted to say. But lets say, you most often get something here that lets you keep going. Not only on tek-tips.

Mike, this is a nice refernce, I wonder why such a bundle of functions is made a class, though I am a big proponent of OOP. Anyway, that's I guess one of the largest bundle I've come across. I haven't counted, yet.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top