Home > Back-end >  Delphi how to add and subtract computing date?
Delphi how to add and subtract computing date?

Time:12-09

I am from a database query out of date, Delphi string type, format is:
S1:='20140101'.
S2:='20140908'.
I think automatic calculation in Delphi minus the difference between the number of days of s1 and s2 add this number to me from the database query to another date above the s3,
Please grant instruction!

CodePudding user response:

Converts the date type, then use DaysBetween (reference DateUtils unit) function calculation of two dates

CodePudding user response:

 var S1, S2: string; 
The begin
S1:='20140101'.
S2:='20140908'.
FieldByName (" date "). AsString:=FormatDateTime (' yyyyMMdd 'StrToDate (S2) - StrToDate (S1));
end;

For reference only.

CodePudding user response:

Error: '20140908' is not a valid date.

CodePudding user response:

S:='20140908'.
Insert (' - 's, 5);
Insert (' - 's, 8).
Formation - dd yyyy - MM
Then strtodate
Then daysbetween

CodePudding user response:

The function cnStrToDateTime (AStr: string) : TDateTime;
Var
Setting: TFormatSettings;
The begin
Setting:=TFormatSettings. Create (LOCALE_USER_DEFAULT);
Setting. ShortDateFormat:='yyyymmdd';
Setting. DateSeparator:='-';
Setting. TimeSeparator:=':';
Setting. LongTimeFormat:='hh: nn: ss. Z';
Result:=StrToDateTime (AStr, Setting);
end;

IncDate (DateBetween (cnStrToDateTime (' 20160101 '), cnStrToDateTime (' 20150203 ')))

CodePudding user response:

IncDate (OtherDate cnStrToDateTime (' 20160101 ') - cnStrToDateTime (' 20150203 '))

CodePudding user response:

The function cnStrToDateTime (AStr: string) : TDateTime;
Var
Setting: TFormatSettings;
The begin
Setting:=TFormatSettings. Create (LOCALE_USER_DEFAULT);
Setting. ShortDateFormat:='yyyymmdd';
Setting the DateSeparator:=';
Setting. TimeSeparator:=':';
Setting. LongTimeFormat:='hh: nn: ss. Z';
Result:=StrToDateTime (AStr, Setting);
end;


IncDate (OtherDate cnStrToDateTime (' 20160101 ') - cnStrToDateTime (' 20150203 '))

CodePudding user response:

refer to the second floor lyhoo163 response:
 var S1, S2: string; 
The begin
S1:='20140101'.
S2:='20140908'.
FieldByName (" date "). AsString:=FormatDateTime (' yyyyMMdd 'StrToDate (S2) - StrToDate (S1));
end;

For reference only.
problem is your date/time parameter is consistent with, or to join:
In the form of software OnCreat events include:

DateSeparator:=';
ShortDateFormat:='yyyyMMdd';
LongDateFormat:='yyyy' years', MM ' 'month', 'dd' ', ' ';
TimeSeparator:=':';
TimeAMString:='AM';
TimePMString:='PM';
ShortTimeFormat:='hh: mm;
LongTimeFormat:='hh: mm: ss';

CodePudding user response:

refer to the eighth floor lyhoo163 response:
Quote: refer to the second floor lyhoo163 response:

 var S1, S2: string; 
The begin
S1:='20140101'.
S2:='20140908'.
FieldByName (" date "). AsString:=FormatDateTime (' yyyyMMdd 'StrToDate (S2) - StrToDate (S1));
end;

For reference only.
problem is your date/time parameter is consistent with, or to join:
In the form of software OnCreat events include:

DateSeparator:=';
ShortDateFormat:='yyyyMMdd';
LongDateFormat:='yyyy' years', MM ' 'month', 'dd' ', ' ';
TimeSeparator:=':';
TimeAMString:='AM';
TimePMString:='PM';
ShortTimeFormat:='hh: mm;
LongTimeFormat:='hh: mm: ss';


Why global???????????????

CodePudding user response:

This is the default Settings of the operating system date/time format, not this code, to operating system Settings can also,

CodePudding user response:

top a learning how to learn ~ ~ ~

CodePudding user response:

Well, learn from teachers

CodePudding user response:

Use TFormatSettings converted to date, and then to addition and subtraction, upstairs existing code, no copy,

CodePudding user response:

A integer part of the data is day, directly add and subtract,
  • Related