//
Unit ads_date;
{Copyright(c)2016 Advanced Delphi Systems
Richard Maley
Advanced Delphi Systems
12613 Maidens Bower Drive
Potomac, MD 20854 USA
phone 301-840-1554
dickmaley@advdelphisys.com
The code herein can be used or modified by anyone. Please retain references
to Richard Maley at Advanced Delphi Systems. If you make improvements to the
code please send your improvements to dickmaley@advdelphisys.com so that the
entire Delphi community can benefit. All comments are welcome.
}
(*UnitIndex Master Index Implementation Section Download UnitsDescription: ads_Date.pas This unit contains the following routines.
Date_DaysInMonth Date_FirstDayOfLastWeek Date_FirstDayOfNextMonth Date_FirstDayOfNextWeek Date_FirstDayOfWeek Date_LastDayOfMonth Date_Month Date_MonthNext Date_MonthPrior Date_MoveNDays Date_NextDay Date_NextWeek Date_PriorDay Date_PriorWeek IsDate NowYYYYMMDDHHSS SetFirstDayNextMonth SetLastDayOfMonth TimeDeltaInMinutes TimeDeltaInMSeconds TimeDeltaInSeconds Today
*)
Interface
Uses
SysUtils, StdCtrls, Dialogs, Forms, ExtCtrls,
Messages, WinProcs, WinTypes, Buttons, Classes,
DB, DBTables, Controls, Grids, {UtilKeys,} IniFiles, Graphics,
ShellAPI, wininet {$IFNDEF WIN32}, ToolHelp{$ENDIF};
{!~ Returns The Number Of Days In The Month}
Function Date_DaysInMonth(DateValue: TDateTime): Integer;
{Returns The First Day Of Last Week, i.e., Sunday, As A TDateTime. If an
error occurs then zero is returned.}
Function Date_FirstDayOfLastWeek(DateValue: TDateTime): TDateTime;
{!~ Returns The First Day Of The Month}
Function Date_FirstDayOfNextMonth(DateValue: TDateTime): TDateTime;
{Returns The First Day Of next Week, i.e., Sunday, As A TDateTime. If an
error occurs then zero is returned.}
Function Date_FirstDayOfNextWeek(DateValue: TDateTime): TDateTime;
{Returns The First Day Of the Week, i.e., Sunday, As A TDateTime. If an
error occurs then zero is returned.}
Function Date_FirstDayOfWeek(DateValue: TDateTime): TDateTime;
{!~ Returns The Last Day Of The Month}
Function Date_LastDayOfMonth(DateValue: TDateTime): TDateTime;
{!~ Returns The Month as an integer when a TDateTime value
is passed as an argument}
Function Date_Month(DateValue: TDateTime): Integer;
{!~ Returns The Next Month}
Function Date_MonthNext(DateValue: TDateTime): Integer;
{!~ Returns The Prior Month}
Function Date_MonthPrior(DateValue: TDateTime): Integer;
{Returns A Date N Days Different Than
The Input Date}
Function Date_MoveNDays(
DateValue : TDateTime;
DateMovement : Integer): TDateTime;
{Returns The Next Day As A TDateTime}
Function Date_NextDay(DateValue: TDateTime): TDateTime;
{!~ Returns The Next Week As A TDateTime}
Function Date_NextWeek(DateValue: TDateTime): TDateTime;
{Returns The Prior Day As A TDateTime}
Function Date_PriorDay(DateValue: TDateTime): TDateTime;
{Returns The Prior Week As A TDateTime}
Function Date_PriorWeek(DateValue: TDateTime): TDateTime;
{!~ Returns True if DateString is a valid date,
False otherwise.}
Function IsDate(DateString: String): Boolean;
{Returns a time delta in minutes}
Function TimeDeltaInMinutes(
StartDate : TDateTime;
EndDate : TDateTime): Double;
{Returns a time delta in seconds}
Function TimeDeltaInSeconds(
StartDate : TDateTime;
EndDate : TDateTime): Double;
{Returns a time delta in Milliseconds}
Function TimeDeltaInMSeconds(
StartDate : TDateTime;
EndDate : TDateTime): Double;
{!~ Returns Today's Date As A String}
Function Today: String;
{!~ Returns Now as DateTime formatted as YYYYMMDDHHSS As a String}
Function NowYYYYMMDDHHSS(): String;
implementation
{Returns The First Day Of Last Week, i.e., Sunday, As A TDateTime. If an
error occurs then zero is returned.}
//Unit Description UnitIndex Master Index
Function Date_FirstDayOfLastWeek(DateValue: TDateTime): TDateTime;
Begin
Result := Date_FirstDayOfWeek(DateValue-7);
End;
{!~ Returns The First Day Of The Month}
//Unit Description UnitIndex Master Index
Function Date_FirstDayOfNextMonth(DateValue: TDateTime): TDateTime;
Begin
Try
Result := Date_LastDayOfMonth(DateValue)+1;
Except
Result := DateValue;
End;
End;
{!~
The following example sets the variable FirstDayNextMonth to
the appropriate TDateTime value associated with DateValue.
//Unit Description UnitIndex Master Index
Procedure SetFirstDayNextMonth(Var FirstDayNextMonth, DateValue : TDateTime);
Begin
FirstDayNextMonth := Date_FirstDayOfNextMonth(DateValue);
End;
}
{Returns The First Day Of next Week, i.e., Sunday, As A TDateTime. If an
error occurs then zero is returned.}
//Unit Description UnitIndex Master Index
Function Date_FirstDayOfNextWeek(DateValue: TDateTime): TDateTime;
Begin
Result := Date_FirstDayOfWeek(DateValue+7);
End;
{Returns The First Day Of the Week, i.e., Sunday, As A TDateTime. If an
error occurs then zero is returned.}
//Unit Description UnitIndex Master Index
Function Date_FirstDayOfWeek(DateValue: TDateTime): TDateTime;
Begin
Try
Result := DateValue - (DayOfWeek(DateValue)) +1;
Except
Result := 0;
End;
End;
{!~ Returns The Last Day Of The Month}
//Unit Description UnitIndex Master Index
Function Date_LastDayOfMonth(DateValue: TDateTime): TDateTime;
Var
LastDay : String;
Begin
{ Result := DateValue;}{zzz}
LastDay := IntToStr(Date_DaysInMonth(DateValue));
Result := StrToDate(
FormatDateTime('mm',DateValue)+
'/'+
LastDay+
'/'+
FormatDateTime('yyyy',DateValue));
End;
{!~
The following example sets the variable LastDayOfMonth to
the appropriate TDateTime value associated with DateValue.
//Unit Description UnitIndex Master Index
Procedure SetLastDayOfMonth(Var LastDayOfMonth, DateValue : TDateTime);
Begin
LastDayOfMonth := Date_LastDayOfMonth(DateValue);
End;
}
{!~ Returns The Month as an integer when a TDateTime value
is passed as an argument}
//Unit Description UnitIndex Master Index
Function Date_Month(DateValue: TDateTime): Integer;
Var
Year, Month, Day: Word;
Begin
Try
DecodeDate(DateValue, Year, Month, Day);
Result := Integer(Month);
Except
Result := -1;
End;
End;
{!~
The following example returns the month as an integer for
1000 days from now.
Date_Month(now()+1000);
}
{!~ Returns The Next Month}
//Unit Description UnitIndex Master Index
Function Date_MonthNext(DateValue: TDateTime): Integer;
Var
Year, Month, Day: Word;
CurMonth : Integer;
NewMonth : Integer;
Begin
Try
DecodeDate(DateValue, Year, Month, Day);
CurMonth := Integer(Month);
NewMonth := ((CurMonth + 12 + 1) mod 12);
If NewMonth = 0 Then NewMonth := 12;
Result := NewMonth;
Except
Result := -1;
End;
End;
{!~
The following example returns the next month as an integer for
1000 days from now.
Date_MonthNext(now()+1000);
}
{!~ Returns The Prior Month}
//Unit Description UnitIndex Master Index
Function Date_MonthPrior(DateValue: TDateTime): Integer;
Var
Year, Month, Day: Word;
CurMonth : Integer;
NewMonth : Integer;
Begin
Try
DecodeDate(DateValue, Year, Month, Day);
CurMonth := Integer(Month);
NewMonth := ((CurMonth + 24 - 1) mod 12);
If NewMonth = 0 Then NewMonth := 12;
Result := NewMonth;
Except
Result := -1;
End;
End;
{!~
The following example returns the prior month as an integer for
1000 days from now.
Date_MonthPrior(now()+1000);
}
{Returns A Date N Days Different Than
The Input Date}
//Unit Description UnitIndex Master Index
Function Date_MoveNDays(
DateValue : TDateTime;
DateMovement : Integer): TDateTime;
Begin
Result := DateValue + DateMovement;
End;
{!~
The following example returns the date as a TDateTime for
1000 days from now.
Date_MoveNDays(now()+1000);
}
{Returns The Next Day As A TDateTime}
//Unit Description UnitIndex Master Index
Function Date_NextDay(DateValue: TDateTime): TDateTime;
Begin
Result := Date_MoveNDays(DateValue,1);
End;
{!~
The following example returns the next day after
1000 days from now.
Date_NextDay(now()+1000);
}
{Returns The Prior Day As A TDateTime}
//Unit Description UnitIndex Master Index
Function Date_PriorDay(DateValue: TDateTime): TDateTime;
Begin
Result := Date_MoveNDays(DateValue,-1);
End;
{!~
The following example returns the prior day after
1000 days from now.
Date_PriorDay(now()+1000);
}
{Returns The Prior Week As A TDateTime}
//Unit Description UnitIndex Master Index
Function Date_PriorWeek(DateValue: TDateTime): TDateTime;
Begin
Result := Date_MoveNDays(DateValue,-7);
End;
{!~
The following example returns the date that
is one week prior to 1000 days from now.
Date_PriorWeek(now()+1000);
}
{!~ Returns True if DateString is a valid date,
False otherwise.}
//Unit Description UnitIndex Master Index
Function IsDate(DateString: String): Boolean;
Begin
Try
StrToDateTime(DateString);
Result := True;
Except
Result := False;
End;
End;
{Returns a time delta in minutes}
//Unit Description UnitIndex Master Index
Function TimeDeltaInMinutes(
StartDate : TDateTime;
EndDate : TDateTime): Double;
Var
Hour : Word;
Min : Word;
Sec : Word;
MSec : Word;
Delta : TDateTime;
Begin
Try
Delta := EndDate - StartDate;
DecodeTime(Delta, Hour, Min, Sec, MSec);
Result := (Hour*60)+Min;
Except
Result := 0;
End;
End;
{Returns a time delta in seconds}
//Unit Description UnitIndex Master Index
Function TimeDeltaInSeconds(
StartDate : TDateTime;
EndDate : TDateTime): Double;
Var
Hour : Word;
Min : Word;
Sec : Word;
MSec : Word;
Delta : TDateTime;
Begin
Try
Delta := EndDate - StartDate;
DecodeTime(Delta, Hour, Min, Sec, MSec);
Result := (((Hour*60)+Min)*60)+Sec;
Except
Result := 0;
End;
End;
{!~ Returns Today's Date As A String}
//Unit Description UnitIndex Master Index
Function Today: String;
Begin
Result := FormatDateTime('m/d/yy',now);
End;
{!~ Returns The Number Of Days In The Month}
//Unit Description UnitIndex Master Index
Function Date_DaysInMonth(DateValue: TDateTime): Integer;
var
YearIn : Word;
MonthIn : Word;
DayIn : Word;
YearNew : Word;
MonthNew : Word;
DayNew : Word;
Counter : Integer;
NewDate : TDateTime;
Begin
Result := 30;
Try
DecodeDate(DateValue, YearIn, MonthIn, DayIn);
NewDate := EncodeDate(YearIn, MonthIn, 26);
For Counter := 26 To 32 Do
Begin
NewDate := NewDate+1;
DecodeDate(NewDate, YearNew, MonthNew, DayNew);
If MonthNew <> MonthIn Then
Begin
DecodeDate(NewDate-1, YearNew, MonthNew, DayNew);
Result := DayNew;
Break;
End;
End;
Except
End;
End;
{Returns The Next Week As A TDateTime}
//Unit Description UnitIndex Master Index
Function Date_NextWeek(DateValue: TDateTime): TDateTime;
Begin
Result := Date_MoveNDays(DateValue,7);
End;
{Returns a time delta in Milliseconds}
//Unit Description UnitIndex Master Index
Function TimeDeltaInMSeconds(
StartDate : TDateTime;
EndDate : TDateTime): Double;
Var
Hour : Word;
Min : Word;
Sec : Word;
MSec : Word;
Delta : TDateTime;
Begin
{ Result := 0;}{zzz}
Try
Delta := EndDate - StartDate;
DecodeTime(Delta, Hour, Min, Sec, MSec);
Result := (((((Hour*60)+Min)*60)+Sec)*1000)+MSec;
Except
Result := 0;
End;
End;
{!~ Returns Now as DateTime formatted as YYYYMMDDHHSS As a String}
//Unit Description UnitIndex Master Index
Function NowYYYYMMDDHHSS(): String;
Begin
Result := FormatDateTime('YYYYMMDDHHSS',now);
End;
End.
//