Solved

Function to return last date of quarter

Posted on 2013-11-23
2
474 Views
Last Modified: 2013-11-23
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
Comment
Question by:Delphiwizard
2 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 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;

Open in new window


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

Open in new window


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.

Open in new window

0
 

Author Closing Comment

by:Delphiwizard
ID: 39672212
Excellent, thank you very much.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use Hashing sha1 in Delphi 2010 4 201
Delphi : could not find program, '...exe' 2 147
find a node in VST 2 63
Unique identifier on a terminal server (rdp) 4 46
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

21 Experts available now in Live!

Get 1:1 Help Now