Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Function to return last date of quarter

Posted on 2013-11-23
Medium Priority
485 Views
Hi,

Does anybody have a function that returns the last date of a quarter based on a given date.

Input: 1-1-2013 -> Return: 31-3-2013
Input: 15-2-2013 -> Return: 31-3-2013
Input: 7-6-2013 -> Return: 30-6-2013
etc.
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

LVL 31

Accepted Solution

Marco Gasi earned 2000 total points
ID: 39671883
You can use this:

``````function GetLastDayOfQuarter(DT: TDateTime): TDateTime;
begin
Result := Now();
if  InRange(MonthOf(DT), 1, 3) then
Result := EndOfAMonth(YearOf(DT), 3)
else
if  InRange(MonthOf(DT), 4, 6) then
Result := EndOfAMonth(YearOf(DT), 6)
else
if  InRange(MonthOf(DT), 7, 9) then
Result := EndOfAMonth(YearOf(DT), 9)
else
if  InRange(MonthOf(DT), 10, 12) then
Result := EndOfAMonth(YearOf(DT), 12);
end;
``````

This is the test program:

Form:
``````object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 219
ClientWidth = 340
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 104
Top = 48
Width = 140
Height = 33
Caption = '00/00/0000'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -27
Font.Name = 'Tahoma'
Font.Style = []
ParentFont = False
end
object DateTimePicker1: TDateTimePicker
Left = 80
Top = 128
Width = 186
Height = 21
Date = 41601.925650833340000000
Time = 41601.925650833340000000
TabOrder = 0
OnChange = DateTimePicker1Change
end
end
``````

Unit:
``````unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, System.DateUtils, System.Math,
Vcl.StdCtrls;

type
TForm1 = class(TForm)
DateTimePicker1: TDateTimePicker;
Label1: TLabel;
procedure DateTimePicker1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{\$R *.dfm}

function GetLastDayOfQuarter(DT: TDateTime): TDateTime;
begin
if  InRange(MonthOf(DT), 1, 3) then
Result := EndOfAMonth(YearOf(DT), 3)
else
if  InRange(MonthOf(DT), 4, 6) then
Result := EndOfAMonth(YearOf(DT), 6)
else
if  InRange(MonthOf(DT), 7, 9) then
Result := EndOfAMonth(YearOf(DT), 9)
else
if  InRange(MonthOf(DT), 10, 12) then
Result := EndOfAMonth(YearOf(DT), 12);
end;

procedure TForm1.DateTimePicker1Change(Sender: TObject);
begin
Label1.Caption := DateToStr(GetLastDayOfQuarter(DateTimePicker1.DateTime));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Label1.Caption := DateToStr(Now());
end;

end.
``````
0

Author Closing Comment

ID: 39672212
Excellent, thank you very much.
0

## Featured Post

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
###### Suggested Courses
Course of the Month11 days, 12 hours left to enroll