Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Function to get previous, current and next quarter based on date

Posted on 2011-03-01
Medium Priority
1,895 Views
Hi,

I need three functions to get previous, current and next quarter based on a date.
Date: 31-12-2010

Function DecQuarter(MyDate : TDateTime) : Integer;
Previous = 3

Function Quarter(MyDate : TDateTime) : Integer;
Current = 4

Function IncQuarter(MyDate : TDateTime) : Integer;
Next = 1
0
Question by:Delphiwizard
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2
• 2
• +1

LVL 15

Expert Comment

ID: 35014856

There are only 4 possible answers for each function:  1, 2, 3 or 4.
Once you know the month, if the month of the date passed to the function is 1, 2 or 3, the quarter of the date is 1; 4, 5 or 6, the quarter is 2; 7, 8 or 9, the quarter is 3 and finally 10, 11 or 12 the quarter is 4.
If you take the current month, add 2 and then divide the result by 3, and discard the fraction, you get the number of the quarter. TRUNC((Month + 2) / 3) = CurrentQuarter
(1+2)/3 = 1
(2+2)/3 = 1.33333 (TRUNCating it discards the .33333)
(3+2)/3 = 1.66666 (TRUNCating it discards the .66666)

For the Previous quarter function, you have to subtract 1 from the resulting number and, if the result of this subtraction is 0, the previous quarter is 4.

For the  Next quarter function, you have to add 1 to the resulting number and, if the result is 5, the next quarter is 1.

I hope that helps.
0

LVL 19

Accepted Solution

Thommy earned 2000 total points
ID: 35014926
``````function Quarter(MyDate:TDateTime):integer;
var
Year, Month, Day, Hour: Word;
begin
DecodeDate(MyDate, Year, Month, Day);
result:=((Month-1) div 3) +1;
end;

function DecQuarter(MyDate:TDateTime):integer;
begin
result:=Quarter(MyDate)-1;
if result=0 then result:=4;
end;

function IncQuarter(MyDate:TDateTime):integer;
begin
result:=Quarter(MyDate)+1;
if result=5 then result:=1;
end;
``````
0

Author Closing Comment

ID: 35014935
Exactly what I needed. Thank you.
0

LVL 10

Expert Comment

ID: 35014967
function Quarter(MyDate: TDateTime): Integer;
var  Y, M, D: Word;
begin
DecodeDate(MyDate, Y, M, D);
Result := ((M-1) div 3) + 1;
end;

function IncQuarter(MyDate: TDateTime): Integer;
var q: Integer;
begin
Result := Quater(MyDate) + 1;
if Result > 4 then
Result := 1;
end;

function DecQuarter(MyDate: TDateTime): Integer;
var q: Integer;
begin
Result := Quater(MyDate) - 1;
if Result < 1 then
Result := 4;
end;
0

LVL 10

Expert Comment

ID: 35014971
lol, too slow :-)
0

Author Comment

ID: 35015079
But thanks anyway, Jacco.
0

LVL 19

Expert Comment

ID: 35015151
Thank you for the points...
:o)))
0

Author Comment

ID: 35015455
Thank you for thanking me :-)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code Â  (CODE) The Imâ€¦
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have toâ€¦
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a qâ€¦
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Prâ€¦
###### Suggested Courses
Course of the Month10 days, 1 hour left to enroll