s1:='20140101';
s2:='20140908';
我想在delphi中自动计算s2减去s1的天数之差然后把这个天数加到我从数据库里面查询到的另外一个日期s3上面去。
求赐教!
14 个解决方案
#1
转换成date类型,然后用DaysBetween(需引用DateUtils单元)函数计算两个日期的差
#2
var S1,S2:string;
begin
s1:='20140101';
s2:='20140908';
FieldByName('日期').AsString:=FormatDateTime('yyyyMMdd',StrToDate(S2)-StrToDate(S1));
end;
仅供参考。
#3
报错:'20140908' is not a valid date.
#4
s:='20140908';
Insert('-', s, 5);
Insert('-', s, 8);
形成yyyy-MM-dd
然后strtodate
然后daysbetween
Insert('-', s, 5);
Insert('-', s, 8);
形成yyyy-MM-dd
然后strtodate
然后daysbetween
#5
function cnStrToDateTime(AStr: string):TDateTime;
var
Setting : TFormatSettings;
begin
Setting := TFormatSettings.Create(LOCALE_USER_DEFAULT);
Setting.ShortDateFormat:='yyyymmdd';
Setting.DateSeparator:='-';
Setting.TimeSeparator:=':';
Setting.LongTimeFormat:='hh:nn:ss.zzz';
Result:= StrToDateTime(AStr, Setting);
end;
IncDate(DateBetween(cnStrToDateTime('20160101'), cnStrToDateTime('20150203')))
var
Setting : TFormatSettings;
begin
Setting := TFormatSettings.Create(LOCALE_USER_DEFAULT);
Setting.ShortDateFormat:='yyyymmdd';
Setting.DateSeparator:='-';
Setting.TimeSeparator:=':';
Setting.LongTimeFormat:='hh:nn:ss.zzz';
Result:= StrToDateTime(AStr, Setting);
end;
IncDate(DateBetween(cnStrToDateTime('20160101'), cnStrToDateTime('20150203')))
#6
IncDate(OtherDate, cnStrToDateTime('20160101') - cnStrToDateTime('20150203'))
#7
function cnStrToDateTime(AStr: string):TDateTime;
var
Setting : TFormatSettings;
begin
Setting := TFormatSettings.Create(LOCALE_USER_DEFAULT);
Setting.ShortDateFormat:='yyyymmdd';
Setting.DateSeparator:='';
Setting.TimeSeparator:=':';
Setting.LongTimeFormat:='hh:nn:ss.zzz';
Result:= StrToDateTime(AStr, Setting);
end;
IncDate(OtherDate, cnStrToDateTime('20160101') - cnStrToDateTime('20150203'))
var
Setting : TFormatSettings;
begin
Setting := TFormatSettings.Create(LOCALE_USER_DEFAULT);
Setting.ShortDateFormat:='yyyymmdd';
Setting.DateSeparator:='';
Setting.TimeSeparator:=':';
Setting.LongTimeFormat:='hh:nn:ss.zzz';
Result:= StrToDateTime(AStr, Setting);
end;
IncDate(OtherDate, cnStrToDateTime('20160101') - cnStrToDateTime('20150203'))
#8
前题是你的日期时间参数是符合的,否则要加入:
在软件的启动窗体的OnCreat事件中加入:
DateSeparator:='';
ShortDateFormat:='yyyyMMdd';
LongDateFormat:='yyyy''年'',MM''月'',dd''日''';
TimeSeparator:=':';
TimeAMString:='AM';
TimePMString:='PM';
ShortTimeFormat:='hh:mm';
LongTimeFormat:='hh:mm:ss';
在软件的启动窗体的OnCreat事件中加入:
DateSeparator:='';
ShortDateFormat:='yyyyMMdd';
LongDateFormat:='yyyy''年'',MM''月'',dd''日''';
TimeSeparator:=':';
TimeAMString:='AM';
TimePMString:='PM';
ShortTimeFormat:='hh:mm';
LongTimeFormat:='hh:mm:ss';
#9
为什么要全局化???????????
#10
这是设置操作系统的日期时间默认格式,不要这些代码,要通过操作系统设置一致也可。
#11
顶一个学习学习~~~
#12
不错,向各位老师学习
#13
用TFormatSettings转换成日期,再进行加减运算。楼上已有代码,不再copy。
#14
data的整数部分就是天,直接加减就行了。
#1
转换成date类型,然后用DaysBetween(需引用DateUtils单元)函数计算两个日期的差
#2
var S1,S2:string;
begin
s1:='20140101';
s2:='20140908';
FieldByName('日期').AsString:=FormatDateTime('yyyyMMdd',StrToDate(S2)-StrToDate(S1));
end;
仅供参考。
#3
报错:'20140908' is not a valid date.
#4
s:='20140908';
Insert('-', s, 5);
Insert('-', s, 8);
形成yyyy-MM-dd
然后strtodate
然后daysbetween
Insert('-', s, 5);
Insert('-', s, 8);
形成yyyy-MM-dd
然后strtodate
然后daysbetween
#5
function cnStrToDateTime(AStr: string):TDateTime;
var
Setting : TFormatSettings;
begin
Setting := TFormatSettings.Create(LOCALE_USER_DEFAULT);
Setting.ShortDateFormat:='yyyymmdd';
Setting.DateSeparator:='-';
Setting.TimeSeparator:=':';
Setting.LongTimeFormat:='hh:nn:ss.zzz';
Result:= StrToDateTime(AStr, Setting);
end;
IncDate(DateBetween(cnStrToDateTime('20160101'), cnStrToDateTime('20150203')))
var
Setting : TFormatSettings;
begin
Setting := TFormatSettings.Create(LOCALE_USER_DEFAULT);
Setting.ShortDateFormat:='yyyymmdd';
Setting.DateSeparator:='-';
Setting.TimeSeparator:=':';
Setting.LongTimeFormat:='hh:nn:ss.zzz';
Result:= StrToDateTime(AStr, Setting);
end;
IncDate(DateBetween(cnStrToDateTime('20160101'), cnStrToDateTime('20150203')))
#6
IncDate(OtherDate, cnStrToDateTime('20160101') - cnStrToDateTime('20150203'))
#7
function cnStrToDateTime(AStr: string):TDateTime;
var
Setting : TFormatSettings;
begin
Setting := TFormatSettings.Create(LOCALE_USER_DEFAULT);
Setting.ShortDateFormat:='yyyymmdd';
Setting.DateSeparator:='';
Setting.TimeSeparator:=':';
Setting.LongTimeFormat:='hh:nn:ss.zzz';
Result:= StrToDateTime(AStr, Setting);
end;
IncDate(OtherDate, cnStrToDateTime('20160101') - cnStrToDateTime('20150203'))
var
Setting : TFormatSettings;
begin
Setting := TFormatSettings.Create(LOCALE_USER_DEFAULT);
Setting.ShortDateFormat:='yyyymmdd';
Setting.DateSeparator:='';
Setting.TimeSeparator:=':';
Setting.LongTimeFormat:='hh:nn:ss.zzz';
Result:= StrToDateTime(AStr, Setting);
end;
IncDate(OtherDate, cnStrToDateTime('20160101') - cnStrToDateTime('20150203'))
#8
var S1,S2:string;
begin
s1:='20140101';
s2:='20140908';
FieldByName('日期').AsString:=FormatDateTime('yyyyMMdd',StrToDate(S2)-StrToDate(S1));
end;
仅供参考。
在软件的启动窗体的OnCreat事件中加入:
DateSeparator:='';
ShortDateFormat:='yyyyMMdd';
LongDateFormat:='yyyy''年'',MM''月'',dd''日''';
TimeSeparator:=':';
TimeAMString:='AM';
TimePMString:='PM';
ShortTimeFormat:='hh:mm';
LongTimeFormat:='hh:mm:ss';
#9
前题是你的日期时间参数是符合的,否则要加入: var S1,S2:string;
begin
s1:='20140101';
s2:='20140908';
FieldByName('日期').AsString:=FormatDateTime('yyyyMMdd',StrToDate(S2)-StrToDate(S1));
end;
仅供参考。
在软件的启动窗体的OnCreat事件中加入:
DateSeparator:='';
ShortDateFormat:='yyyyMMdd';
LongDateFormat:='yyyy''年'',MM''月'',dd''日''';
TimeSeparator:=':';
TimeAMString:='AM';
TimePMString:='PM';
ShortTimeFormat:='hh:mm';
LongTimeFormat:='hh:mm:ss';
为什么要全局化???????????
#10
这是设置操作系统的日期时间默认格式,不要这些代码,要通过操作系统设置一致也可。
#11
顶一个学习学习~~~
#12
不错,向各位老师学习
#13
用TFormatSettings转换成日期,再进行加减运算。楼上已有代码,不再copy。
#14
data的整数部分就是天,直接加减就行了。