How to calculate the number of weeks in a certain period of time?

Source: Internet
Author: User
How to calculate the number of weeks in a certain period of time? Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiBase/html/delphi_20061216151111140.html
It is known that date A is A day on Tuesday, Thursday, and day. How many Tuesday, four, and day are there between A and B?

{Function parameter description: A -- The Old Date B -- The Nearest Date Dw -- Description of The week to be counted}
{Dw = 1 count the number of Sundays
Dw = 2 count Monday count
Dw = 3 count Tuesday count
Dw = 4 count Wednesday count
Dw = 5 count the number of Thursday
Dw = 6 count Friday count
Dw = 7 count the number of Saturdays}

Function MyFuc (A, B: TDateTime; Dw: integer): integer;
Var
X, Y, Z: integer;
Begin
X: = DayOfWeek ();
{If not (X in [, 4]) then // if you need to restrict date A only
Begin // it is Tuesday, Thursday
Result: =-1; // you can delete
Exit; // This judgment statement is enabled
End;} //. Therefore,-1 is returned instead of Tuesday 4.
Z: = Round (B-A) mod 7;
If Z + X <= 7 then
If (Dw> = X) and (Dw <= Z + X) then Y: = 1 else Y: = 0
Else
If Z + X-7> = Dw then Y: = 1
Else if (Dw <= Z + X) and (Dw> = X) then Y: = 1 else Y: = 0;

Result: = Round (B-A) div 7 + Y;
End;

Procedure TForm1.Button1Click (Sender: TObject); // call the function example
Var
A, B: TDateTime;
Begin
A: = VarToDatetime ('2017-11-18 ');
B: = VarToDatetime ('2017-12-16 ');
ShowMessage (inttostr (MyFuc (a, B, 1) + 'sunday' + #13
+ Inttostr (MyFuc (a, B, 2) + 'monday' + #13
+ Inttostr (MyFuc (a, B, 3) + 'tuesday '+ #13
+ Inttostr (MyFuc (a, B, 4) + 'wednes' + #13
+ Inttostr (MyFuc (a, B, 5) + 'thurs' + #13
+ Inttostr (MyFuc (a, B, 6) + 'Friday' + #13
+ Inttostr (MyFuc (a, B, 7) + 'saturday ');
End;

Note that there is a small error in the judgment part of the note, that is, you can change the set [, 4] to [, 5 !!

Test results ~ 2006-12-16)

Four Sundays
Four Mondays
Four Tuesday
4 Wednesday
4 Thursday
Four Friday
Five Saturdays

Thank you for your reply. "How many Tuesday, Thursday, and Thursday are there between A and B? "It refers to the total number, which does not need to be separated. The following is my method. Please advise.
Procedure TForm1.Button1Click (Sender: TObject );
Var I, j, k: integer;
Begin
I: = daysbetween (vartodatetime (edit1.Text), vartodatetime (edit2.Text ));
J: = dayoftheweek (vartodatetime (edit1.Text ));
K: = I div 7;
I: = I mod 7;
If j = 2 then I: = (I + 1) div 3
Else if j = 4 then I := (I-1) div 2
Else if j = 7 then begin
If I> 4 then I: = 4;
I: = I div 2;
End;
J: = k * 3 + I;
Edit3.Text: = inttostr (j );
End;

I am so grateful that I can boldly go back to my post, so I don't have to thank you. However, "advice" is even more difficult.

Tested the following code: (Tuesday )~ Result: 8

And if I use my function:
Var
A, B: TDateTime;
Begin
A: = VarToDatetime (Edit1.Text); // Edit1.text: = '2017-12-12'
B: = VarToDatetime (Edit2.Text); // Edit2.text: = '2017-12-31'
ShowMessage (IntToStr (MyFuc (a, B, 1) + MyFuc (a, B, 3) + MyFuc (a, B, 5 )));
End;
Test result: 9

When I saw my question, I thought: I should write a common function as much as possible. This function not only counts "Tuesday, Thursday, and Sunday ", instead, we should be able to make statistics on the day of the week. So I wrote it like that.
As for the "sum" you mentioned, it can be solved simply by the most common addition in arithmetic: MyFuc (a, B, 1) + MyFuc (a, B, 3) + MyFuc (a, B, 5)

I hope the landlord will forgive me for my improper remarks.

Oh, by the way, my code is not counted from day A, because it is known to be Tuesday, Thursday, and day. That is, the time period after the B-A.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.