Solved

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

Posted on 2011-03-01
8
1,708 Views
Last Modified: 2012-05-11
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
Comment
Question by:Delphiwizard
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 15

Expert Comment

by:WalkaboutTigger
ID: 35014856
Hint 1:  http://delphi.about.com/od/beginners/l/blrtldatetime.htm
Hint 2:  http://delphi.about.com/library/rtl/blrtlDecodeDate.htm:

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

by:
Thommy earned 500 total points
ID: 35014926
These are your required functions...
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;

Open in new window

0
 

Author Closing Comment

by:Delphiwizard
ID: 35014935
Exactly what I needed. Thank you.
0
 
LVL 10

Expert Comment

by:Jacco
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

by:Jacco
ID: 35014971
lol, too slow :-)
0
 

Author Comment

by:Delphiwizard
ID: 35015079
But thanks anyway, Jacco.
0
 
LVL 19

Expert Comment

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

Author Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now