Board index » delphi » Help needed with TDateTime

Help needed with TDateTime

Hi there

I need a function that will increase the value in a TDateTime with a
number of months

e.g.

03-05-99 + 1 month --> 03-06-99

23-12-99 + 4 months --> 23-03-00

Is this possible

Thanks in advance

Farouche

 

Re:Help needed with TDateTime


I have making this function to do that

function MoinsMois(const JourCourant : TDateTime) : TDateTime;

var Otr_Mois,Otr_Annee : Integer;

begin

Otr_Mois:=RenvoiNombre(FormatDateTime('mm',JourCourant));

Otr_Annee:=RenvoiNombre(FormatDateTime('yyyy',JourCourant));

if Otr_Mois>1

then Dec(Otr_Mois)

else begin

Otr_Mois:=12;

Dec(Otr_Annee);

end;

MoinsMois:=EncodeDate(Otr_Annee,Otr_Mois,1);

end;

function PlusMois(const JourCourant : TDateTime) : TDateTime;

var Otr_Mois,Otr_Annee : Integer;

begin

Otr_Mois:=RenvoiNombre(FormatDateTime('mm',JourCourant));

Otr_Annee:=RenvoiNombre(FormatDateTime('yyyy',JourCourant));

if Otr_Mois<12

then Inc(Otr_Mois)

else begin

Otr_Mois:=1;

Inc(Otr_Annee);

end;

PlusMois:=EncodeDate(Otr_Annee,Otr_Mois,1);

end;

Farouche a crit dans le message <372C63A6.7EB51...@mail1.stofanet.dk>...

Quote
>Hi there

>I need a function that will increase the value in a TDateTime with a
>number of months

>e.g.

>03-05-99 + 1 month --> 03-06-99

>23-12-99 + 4 months --> 23-03-00

>Is this possible

>Thanks in advance

>Farouche

Re:Help needed with TDateTime


JRS:  In article <372C63A6.7EB51...@mail1.stofanet.dk> of Sun, 2 May
1999 16:39:34 in news:comp.lang.pascal.delphi.misc, Farouche

Quote
<a...@mail1.stofanet.dk> wrote:
>Hi there

>I need a function that will increase the value in a TDateTime with a
>number of months

>e.g.

>03-05-99 + 1 month --> 03-06-99

>23-12-99 + 4 months --> 23-03-00

And what do you put for one month after the 31st of Jan, Mar, May, Aug,
Oct?

--
 ? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
  Web <URL: http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
  Dates - miscdate.htm  Year 2000 - date2000.htm  Critical Dates - critdate.htm
  Y2k for beginners - year2000.txt  UK mini-FAQ - y2k-mfaq.txt  Don't Mail News

Re:Help needed with TDateTime


Hi Farouche!
Try this:

procedure IncreaseMonths(var aDate : TDateTime; count : WORD);
var y, m, d : WORD;
begin
  // Get a normal Date
  DecodeDate(aDate, y, m, d);

  // Increase Month
  INC(m, count);

  // Check, if there is a valid Date
  While m > 12 do
   begin
     DEC(m, 12);
     INC(y);
   end;

  // OK, now Encode the Date again!
  aDate := EncodeDate(y, m, d);
end;

Michael Friedl

Re:Help needed with TDateTime


Quote
Farouche <a...@mail1.stofanet.dk> wrote in message news:372C63A6.7EB51749@mail1.stofanet.dk...
> Hi there

> I need a function that will increase the value in a TDateTime with a
> number of months

>...

> Farouche

Hi Farouche,

At the end of this message is a unit, SPxFuncs.pas.

This is part of a unit I use a LOT!, it is full of commonly used routines for date,
time, bitstates, rectangles and windows, etc.

Just extract any routines you need, or better still, put it in your library dir and use it.

The function you are after is dateIncMonth();

Hope this is of some help

Simon Patterson

Select text from here to end, copy and paste into Notepad. Save as SPxFuncs.pas. Put in Lib dir and
use.
{-----------------------------------------------------------------------------}
{  SPXFUNCS.PAS                                                               }
{                                                                             }
{  Delphi VCL - SPx Components Useful Program Routines                        }
{                                                                             }
{  Simon Patterson, SPS Software Solutions                                    }
{-----------------------------------------------------------------------------}
{ N.B. This is a small version of SPXFUNCS. (76 routines)                     }
unit SPxFuncs;

interface

uses
  Windows, SysUtils;

{ Date routines }
function dateMin(const ADate1, ADate2: TDateTime): TDateTime;
function dateMax(const ADate1, ADate2: TDateTime): TDateTime;
function dateMinN(const Dates: array of TDateTime): TDateTime;
function dateMaxN(const Dates: array of TDateTime): TDateTime;
function dateExtractDate(const ADate: TDateTime): TDateTime; { sets time to 00:00:00:00 }
function dateExtractDay(const ADate: TDateTime): Word;
function dateExtractMonth(const ADate: TDateTime): Word;
function dateExtractYear(const ADate: TDateTime): Word;
function dateBeginOfYear(const ADate: TDateTime): TDateTime;
function da{*word*249}dOfYear(const ADate: TDateTime): TDateTime;
function dateDayOfYear(const ADate: TDateTime): Integer;
function dateFirstDayOfNextMonth(const ADate: TDateTime): TDateTime;
function dateFirstDayOfPrevMonth(const ADate: TDateTime): TDateTime;
function dateLastDayOfNextMonth(const ADate: TDateTime): TDateTime;
function dateLastDayOfPrevMonth(const ADate: TDateTime): TDateTime;
function dateIsYearLeapYear(const AYear: Integer): Boolean;
function dateIsLeapYear(const ADate: TDateTime): Boolean;
function dateDay{*word*237}onth(const AYear, AMonth: Integer): Integer;
function dateDaysThisMonth(const ADate: TDateTime): Integer;
function dateDaysBetween(const ADate1, ADate2: TDateTime): Longint;
function dateWorkingDaysBetween(const ADate1, ADate2: TDateTime): Longint;
function dateWholeMonthsBetween(const ADate1, ADate2: TDateTime): Integer;
function dateIncDate(const ADate: TDateTime; ADays, AMonths, AYears: Integer): TDateTime;
function dateIncDay(const ADate: TDateTime; ADay: Integer): TDateTime;
function dateIncMonth(const ADate: TDateTime; AMonth: Integer): TDateTime;
function dateIncYear(const ADate: TDateTime; AYear: Integer): TDateTime;
procedure dateSwap(var ADate1, ADate2: TDateTime);
procedure dateDiff(ADate1, ADate2: TDateTime; var Days, Months, Years: Word);
function dateWeekOfYear(const ADate: TDateTime): Integer;
function dateCompare(const Value1, Value2: TDateTime): Integer;

{ Time routines }
function timeExtractTime(const ATime: TDateTime): TDateTime; { sets date to 00/00/0000 }
function timeExtractHour(const ATime: TDateTime): Word;
function timeExtractMin(const ATime: TDateTime): Word;
function timeExtractSec(const ATime: TDateTime): Word;
function timeExtractMSec(const ATime: TDateTime): Word;
function timeIncTime(const ATime: TDateTime; AHours, AMinutes, ASeconds, AMSecs: Integer):
TDateTime;
function timeIncHour(const ATime: TDateTime; AHours: Integer): TDateTime;
function timeIncMin(const ATime: TDateTime; AMinutes: Integer): TDateTime;
function timeIncSec(const ATime: TDateTime; ASeconds: Integer): TDateTime;
function timeIncMSec(const ATime: TDateTime; AMSecs: Integer): TDateTime;
function timeToInt(const ATime: TDateTime): Integer;
procedure timeSwap(var ATime1, ATime2: TDateTime);
procedure timeDiff(Time1, Time2: TDateTime; var Hours, Minutes, Seconds, MSeconds: Word);
function timeCompare(const Value1, Value2: TDateTime): Integer;

{ Integer routines }
function intMin(const Value1, Value2: Integer): Integer;
function intMax(const Value1, Value2: Integer): Integer;
function intMinN(const Values: array of Integer): Integer;
function intMaxN(const Values: array of Integer): Integer;
function intInRange(const Value, LowBound, UpBound: LongInt): Boolean;
function intLimitToRange(const Value, LowBound, UpBound: LongInt): LongInt;
function intSign(const Value: Integer): Integer;
function intCompare(const Value1, Value2: Integer): Integer;
function intMirror(const Value: Integer): Integer;
function intPWR(const Base, Exponent: Integer): Integer;
procedure intSwap(var Value1, Value2: Integer);
function intOrdinalNumberSuffix(const Value: Integer): string; { returns st, nd, rd etc }

{ Floating Point functions }
function floatMin(const Value1, Value2: Extended): Extended;
function floatMax(const Value1, Value2: Extended): Extended;
function floatMinN(const Values: array of Extended): Extended;
function floatMaxN(const Values: array of Extended): Extended;
function floatInRange(const Value, LowBound, UpBound: Extended): Boolean;
function floatLimitToRange(const Value, LowBound, UpBound: Extended): Extended;
function floatSign(const Value: Extended): Integer;
function floatCompare(const Value1, Value2: Extended): Integer;
function floatPWR(const Base, Exponent: Extended): Extended;
procedure floatSwap(var Value1, Value2: Extended);
function floatLog(const Value: Extended): Extended;     { returns common logarithm of x }
function floatLog2(const Value: Extended): Extended;     { returns the 2_Log(x) value }

{ Bitwise functions }
function bitState(const A, BitNo: Integer): Boolean;
function bitSet(const A, BitNo: Integer; State: Boolean): Integer;

{ Rectangle functions }
function rectWidth(const R: TRect): Integer;
function rectHeight(const R: TRect): Integer;
function rectPointIn(const R: TRect; P: TPoint): Boolean;

{ Window routines }
procedure windowCentreOnScreen(const HWindow: HWnd);
procedure windowCentreWindow(const HWindow: HWnd);
procedure windowSetTopMost(const HWindow: HWnd; OnTop: Boolean);

{-----------------------------------------------------------------------------}
implementation
{-----------------------------------------------------------------------------}

{-----------------------------------------------------------------------------}
{  Date routines                                                              }
{-----------------------------------------------------------------------------}

function dateMin(const ADate1, ADate2: TDateTime): TDateTime;
begin
  if ADate1 <= ADate2 then Result := ADate1
  else Result := ADate2;
end;

function dateMax(const ADate1, ADate2: TDateTime): TDateTime;
begin
  if ADate1 >= ADate2 then Result := ADate1
  else Result := ADate2;
end;

function dateMinN(const Dates: array of TDateTime): TDateTime;
var
  i: Cardinal;
begin
  Result := Dates[Low(Dates)];
  for i := 0 to High(Dates) do if Dates[i] < Result then Result := Dates[i];
end;

function dateMaxN(const Dates: array of TDateTime): TDateTime;
var
  i: Cardinal;
begin
  Result := Dates[Low(Dates)];
  for i := 0 to High(Dates) do if Dates[i] < Result then Result := Dates[i];
end;

function dateExtractDate(const ADate: TDateTime): TDateTime;
begin
  Result := Trunc(ADate);  // remove time
end;

function dateExtractDay(const ADate: TDateTime): Word;
var
  M, Y: Word;
begin
  DecodeDate(ADate, Y, M, Result);
end;

function dateExtractMonth(const ADate: TDateTime): Word;
var
  D, Y: Word;
begin
  DecodeDate(ADate, Y, Result, D);
end;

function dateExtractYear(const ADate: TDateTime): Word;
var
  D, M: Word;
begin
  DecodeDate(ADate, Result, M, D);
end;

function dateBeginOfYear(const ADate: TDateTime): TDateTime;
var
  Y, M, D: Word;
begin
  DecodeDate(ADate, Y, M, D);
  Result := EncodeDate(Y, 1, 1);
end;

function da{*word*249}dOfYear(const ADate: TDateTime): TDateTime;
var
  Y, M, D: Word;
begin
  DecodeDate(ADate, Y, M, D);
  Result := EncodeDate(Y, 12, 31);
end;

function dateDayOfYear(const ADate: TDateTime): Integer;
begin
  Result := Trunc(ADate - dateBeginOfYear(ADate)) + 1;
end;

function dateFirstDayOfNextMonth(const ADate: TDateTime): TDateTime;
var
  Y, M, D: Word;
begin
  DecodeDate(ADate, Y, M, D);
  D := 1;
  if M < 12 then
    Inc(M)
  else begin
    M := 1;
    Inc(Y);
  end;
  Result := EncodeDate(Y, M, D);
end;

function dateFirstDayOfPrevMonth(const ADate: TDateTime): TDateTime;
var
  Y, M, D: Word;
begin
  DecodeDate(ADate, Y, M, D);
  D := 1;
  if M > 1 then
    Dec(M)
  else begin
    M := 12;
    Dec(Y);
  end;
  Result := EncodeDate(Y, M, D);
end;

function dateLastDayOfNextMonth(const ADate: TDateTime): TDateTime;
var
  Y, M, D: Word;
begin
  DecodeDate(dateFirstDayOfNextMonth(ADate), Y, M, D);
  Result := EncodeDate(Y, M, dateDay{*word*237}onth(Y, M));
end;

function dateLastDayOfPrevMonth(const ADate: TDateTime): TDateTime;
var
  Y, M, D: Word;
begin
  DecodeDate(dateFirstDayOfPrevMonth(ADate), Y, M, D);
  Result := EncodeDate(Y, M, dateDay{*word*237}onth(Y, M));
end;

function dateIsYearLeapYear(const AYear: Integer): Boolean;
begin
  Result := (AYear mod 4 = 0) and ((AYear mod 100 <> 0) or (AYear mod 400 = 0));
end;

function dateIsLeapYear(const ADate: TDateTime): Boolean;
var
  Y, D, M: Word;
begin
  DecodeDate(ADate, Y, M, D);
  Result := dateIsYearLeapYear(Y);
end;

function dateDay{*word*237}onth(const AYear, AMonth: Integer): Integer;
const
  DaysInMonth: array[1..12] of Integer = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
begin
  Result := DaysInMonth[AMonth];
  if (AMonth = 2) and dateIsYearLeapYear(AYear) then
...

read more »

Re:Help needed with TDateTime


  "Simon Biddle" <simon.bid...@clear.co.nz> wrote:

Quote
> Speaking of which - Delphi 1 doesn't process Feb 29, 2000 correctly. Does
> anyone know more on the subject?

Can you be more specific? I have been working with D1 and calendar programs
for a while now & it seems perfectly OK to me.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Help needed with TDateTime


Just adding to the months and then MOD/DIVing to correct for a year change
is not enough. You're going to need something to take care of the difference
in length of the months. ie:
30-12-99 + 2 months --> 30-02-00
...won't work (Feb only ever has 28-29 days). Remeber as well that Feb 2000
IS a leap year, whereas 1900 and 2100 aren't (if you want to be completely
correct, check all the leap year rules...).

Speaking of which - Delphi 1 doesn't process Feb 29, 2000 correctly. Does
anyone know more on the subject?

Re:Help needed with TDateTime


In D4, there's a routine called IncMonth (part of SysUtils).

John.

Quote
Farouche <a...@mail1.stofanet.dk> wrote in message

news:372C63A6.7EB51749@mail1.stofanet.dk...
Quote
> Hi there

> I need a function that will increase the value in a TDateTime with a
> number of months

> e.g.

> 03-05-99 + 1 month --> 03-06-99

> 23-12-99 + 4 months --> 23-03-00

> Is this possible

> Thanks in advance

> Farouche

Other Threads