program ConvertDateTimeToTicksProject;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.TimeSpan;
function ConvertTimeStampToMilliSeconds(
const ATimeStamp: TTimeStamp): Int64;
begin
Result := ATimeStamp.Date - 1;
Result := (Result * MSecsPerDay) + ATimeStamp.Time;
end;
function ConvertDateTimeToTicks(
const ADateTime: TDateTime): Int64;
var
TimeStamp: TTimeStamp;
TimeStampInMilliseconds: Int64;
begin
TimeStamp := DateTimeToTimeStamp(ADateTime);
TimeStampInMilliseconds := ConvertTimeStampToMilliSeconds(TimeStamp);
Result := TimeStampInMilliseconds * TTimeSpan.TicksPerMillisecond;
end;
function ConvertDateTimeToTicksEx(
const ADateTime: TDateTime): Int64;
var
TimeSpan: TTimeSpan;
LHour, LMinute, LSecond, LMilliSecond: Word;
LDays: Integer;
begin
LDays := DateTimeToTimeStamp(ADateTime).Date - 1;
DecodeTime(ADateTime, LHour, LMinute, LSecond, LMilliSecond);
TimeSpan := TTimeSpan.Create(LDays, LHour, LMinute, LSecond, LMilliSecond);
Result := TimeSpan.Ticks;
end;
procedure Main;
var
MyDateTime: TDateTime;
begin
MyDateTime := System.SysUtils.Now;
Writeln(FormatDateTime('
hh:nn:ss:zzz - dd.mm.yyyy', MyDateTime));
Writeln(ConvertDateTimeToTicks(MyDateTime));
MyDateTime := StrToDateTime('
01.04.2017 00:00:01');
Writeln(FormatDateTime('
hh:nn:ss:zzz - dd.mm.yyyy', MyDateTime));
Writeln(ConvertDateTimeToTicks(MyDateTime));
MyDateTime := StrToDateTime('
29.02.2020 12:00:00');
Writeln(FormatDateTime('
hh:nn:ss:zzz - dd.mm.yyyy', MyDateTime));
Writeln(ConvertDateTimeToTicks(MyDateTime));
/// //////////////////////////////////////////////////////////////
MyDateTime := System.SysUtils.Now;
Writeln(FormatDateTime('
hh:nn:ss:zzz - dd.mm.yyyy', MyDateTime));
Writeln(ConvertDateTimeToTicksEx(MyDateTime));
MyDateTime := StrToDateTime('
01.04.2017 00:00:01');
Writeln(FormatDateTime('
hh:nn:ss:zzz - dd.mm.yyyy', MyDateTime));
Writeln(ConvertDateTimeToTicksEx(MyDateTime));
MyDateTime := StrToDateTime('
29.02.2020 12:00:00');
Writeln(FormatDateTime('
hh:nn:ss:zzz - dd.mm.yyyy', MyDateTime));
Writeln(ConvertDateTimeToTicksEx(MyDateTime));
end;
begin
try
Main;
Readln;
except
on E:
Exception do
Writeln(E.ClassName, '
: ', E.
Message);
end;
end.